An End of File Character for the FOR loop?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

An End of File Character for the FOR loop?

#1 Post by PaperTronics » 21 Sep 2017 07:08

Hey guys!

So, recently I've been doing a lot of file handling in Batch, and I came across a problem I couldn't solve.

I wanted to have an end of file character for the FOR loop, that works similar to the eol character function, but instead of going to the next line after the eol character is found, the eof (end of file) character will simply end the for loop.


After reading about my problem, the first thing that would've came to your mind is using the IF command, well, I tried the IF command and it didn't work. So if anyone has any other solution, the help will be greatly appreciated.



Thanks,
PaperTronics

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: An End of File Character for the FOR loop?

#2 Post by ShadowThief » 21 Sep 2017 09:24

What did your if statement look like?

Code: Select all

if "!character!"=="!eof_character!" goto :some_label_outside_of_the_loop

will definitely break out of the loop.

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: An End of File Character for the FOR loop?

#3 Post by PaperTronics » 23 Sep 2017 06:25

My IF Statement looks similar to yours:


Code: Select all

if "%%A"== "}" (goto :a)


But it doesn't work. Is something wrong with my statement?



Thanks for your help,
PaperTronics

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: An End of File Character for the FOR loop?

#4 Post by aGerman » 23 Sep 2017 08:55

I can only quote myself repeatedly - We are not in front of your screen and thus, we don't see what you're seeing and we don't know what you're knowing. You didn't provide your script and you didn't provide the text file that you try to process. All that we can do is guessing. I'm not willing to ask a hundred times in order to get all the important informations that you omitted to tell us. There is a sticky post at the beginning of the topic list that you already should have read.

Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: An End of File Character for the FOR loop?

#5 Post by Squashman » 23 Sep 2017 09:09

PaperTronics wrote:My IF Statement looks similar to yours:


Code: Select all

if "%%A"== "}" (goto :a)


But it doesn't work. Is something wrong with my statement?



Thanks for your help,
PaperTronics

Remove the space in the comparison after the equals symbols.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: An End of File Character for the FOR loop?

#6 Post by aGerman » 23 Sep 2017 09:18

... and if the lines he wants to process look like ...

Code: Select all

{foo
bar}
baz
... it will still fail.

Steffen

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: An End of File Character for the FOR loop?

#7 Post by PaperTronics » 24 Sep 2017 03:28

@aGerman - I'm really sorry. I was in a bit of hurry, and couldn't c/p the code.


Here's the code :

Code: Select all

for /f "tokens=*" %%A IN (2017_I/E_Summary.txt) DO (
   Set FileName=%%A
   if "%%A"=="}" (goto :FilePrint)
   )


I'm a bit of clumsy in FOR loops so I think the problem is probably in my loop, but still here's the content of my 2017_I/E_Summary.txt file:



Code: Select all

IMPORT EXPORT DETAILS 2017

Export Details}

2157a45e9b - 15-4-2017 - To Unknown
541288t918b9s - 25-7-2017 - To KDM
--

Import Details}

124j19248I192L - 14-9-2017 - From Lahore






Thanks and sorry,
PaperTronics

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: An End of File Character for the FOR loop?

#8 Post by aGerman » 24 Sep 2017 04:38

PaperTronics wrote:content of my 2017_I/E_Summary.txt file:

That can't be the name of your file because the / is forbidden. If this is rather a relative path where 2017_I is a subfolder then / should have been a \.

However I left out the / and created a file "2017_IE_Summary.txt".

Code: Select all

@echo off &setlocal DisableDelayedExpansion
for /f "usebackq tokens=*" %%i in ("2017_IE_Summary.txt") do (
  set "line=%%i"
  setlocal EnableDelayedExpansion
    echo !line!
    if "!line!" neq "!line:}=!" (endlocal &goto endloop)
    endlocal
)
:endloop
pause

It creates the output...

Code: Select all

IMPORT EXPORT DETAILS 2017
Export Details}


Steffen

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: An End of File Character for the FOR loop?

#9 Post by PaperTronics » 24 Sep 2017 07:49

@aGerman - Thanks! It works perfectly!
Also the name of my file was actually 2017_I.E_Summary.txt but accidentally I wrote / Even in the code, the name was in a variable but I removed the variable and wrote the name down manually, hence committing a mistake. That's what happens when you try to multi-task!





Thanks again,
PaperTronics

Post Reply