An End of File Character for the FOR loop?
Moderator: DosItHelp
-
- Posts: 118
- Joined: 02 Apr 2017 06:11
An End of File Character for the FOR loop?
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
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
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: An End of File Character for the FOR loop?
What did your if statement look like?
will definitely break out of the loop.
Code: Select all
if "!character!"=="!eof_character!" goto :some_label_outside_of_the_loop
will definitely break out of the loop.
-
- Posts: 118
- Joined: 02 Apr 2017 06:11
Re: An End of File Character for the FOR loop?
My IF Statement looks similar to yours:
But it doesn't work. Is something wrong with my statement?
Thanks for your help,
PaperTronics
Code: Select all
if "%%A"== "}" (goto :a)
But it doesn't work. Is something wrong with my statement?
Thanks for your help,
PaperTronics
Re: An End of File Character for the FOR loop?
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
Steffen
Re: An End of File Character for the FOR loop?
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.
Re: An End of File Character for the FOR loop?
... and if the lines he wants to process look like ...... it will still fail.
Steffen
Code: Select all
{foo
bar}
baz
Steffen
-
- Posts: 118
- Joined: 02 Apr 2017 06:11
Re: An End of File Character for the FOR loop?
@aGerman - I'm really sorry. I was in a bit of hurry, and couldn't c/p the code.
Here's the code :
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:
Thanks and sorry,
PaperTronics
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
Re: An End of File Character for the FOR loop?
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
-
- Posts: 118
- Joined: 02 Apr 2017 06:11
Re: An End of File Character for the FOR loop?
@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
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