PLEASE HELP ME WITH "FOR" (I need to know!)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

PLEASE HELP ME WITH "FOR" (I need to know!)

#1 Post by Rileyh » 19 Oct 2011 00:29

I need someone to help me understand the "for" command! (end my suffering of misunderstanding!!)
I can never grasp the concept of the "for" commands syntax, how it works, what it does, everything!
Most especially the "for /f" variation.

Could someone end my suffering, please!


It would be very helpful,

Regards,
Rileyh

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: PLEASE HELP ME WITH "FOR" (I need to know!)

#2 Post by trebor68 » 21 Oct 2011 03:11

The following code is running correct from the prompt. If you want to run this from a batch file use %%a.

file names in this folder
FOR %var IN (set of file names) DO command [parameters]

folder names in this folder
FOR /D %var IN (set of folder names) DO command [parameters]

all file names also in subfolders
FOR /R [[drive:]path] %var IN (set of file names) DO command [parameters]

with numbers
FOR /L %var IN (start number,increment,end number) DO command [parameters]

And now with the parameter /F
FOR /F ["options"] %var IN (set of files) DO command [parameters]
FOR /F ["options"] %var IN ("string") DO command [parameters]
FOR /F ["options"] %var IN (command) DO command [parameters]

For the following command I will use the file "testfile.txt":

Code: Select all

00000000011111111112222222222333333333344444444445
12345678901234567890123456789012345678901234567890
[ beginn of test ]
03.09.2011  18:59             1.687 Arbeit.bat
29.08.2011  18:31               542 Versuch.bat
Candy Blond  #  771 772  #  Czech republic
a0152#a0498#a0573#a0565#a0153#
[ end of test ]


Code: Select all

for /f %a in (testfile.txt) do @echo %a

Display only the characters before the first space is comming.

Code: Select all

for /f "tokens=1-3" %a in (testfile.txt) do @echo "%a" "%b" "%c"

Display the first three words. To understand: space word1 space word2 space word3 space ...

Code: Select all

for /f "tokens=1-2,4" %a in (testfile.txt) do @echo "%a" "%b" "%c"

Display the three words: word1, word2, word4
Token 1 is var %a, token 2 is var %b and token 4 is var %c.
If you want to use a other var as %a in example %p then is valid code:

Code: Select all

for /f "tokens=1-2,4" %p in (testfile.txt) do @echo "%p" "%q" "%r"


With the option "skip=x" you dont see the first x lines.

Code: Select all

for /f "tokens=1-4 skip=2" %a in (testfile.txt) do @echo "%a" "%b" "%c" "%d"


With the option "eol=x" you dont see the line that is beginning with the charakter "x". Only one character is possible.

Code: Select all

for /f "tokens=1-4 skip=2 eol=[" %a in (testfile.txt) do @echo "%a" "%b" "%c" "%d"


You can see now the lines number 4 to 7. The lines number 4 and 5 can you now understand.
But the line 6 will have three areas. One for the name, one for numbers and one for country.
In the line 7 you have five areas. Code1, sign "#", code2, sign "#", code3 ...

With the option "delims=xxx" can you use other separator and not only the space. If you want different seperators inclusive the space than use the space character as the last "delims=# " (here the seperators "#" and space).

Code: Select all

for /f "tokens=1-4 skip=2 eol=[ delims=#" %a in (testfile.txt) do @echo "%a" "%b" "%c" "%d"


If you want the complete line in a variable than can you use:
for /f "tokens=1 skip=2 eol=[ delims=" %a in (testfile.txt) do @echo "%a"
The option delims will not have one character.
HINT: Use the option delims as the last option.

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

Re: PLEASE HELP ME WITH "FOR" (I need to know!)

#3 Post by aGerman » 22 Oct 2011 05:28

trebor68's examples are made to use them directly in a cmd window. If you need to use them in a batch file you will have to double the percent signs of the FOR variables (eg. %a --> %%a).

If you want to work with FOR /F you primarily will have to learn how to use tokens and delims.
Perhaps this batch code could help:

Code: Select all

@echo off
echo delims means delimiter(s) or separator(s)
echo   -the default delimiters are space and tab
echo   -you have to use "delims=" to avoid that a string will be splitted
echo    on these characters
echo   -you could use several delimiters, but they are always parsed as
echo    single characters (not as a word)
echo   -all delimiters are removed from the string
echo   -if a delimiter is followed by another delimiter they are
echo    parsed like a single delimiter
echo   -delimiters in the beginnig of a string are removed and ignored
echo(
echo tokens are the substrings
echo   -if you need more than only the first token you have to define them
echo   -the first (named) token will be saved in the FOR variable
echo    you have defined
echo   -the next (named) token will be saved in the variable with
echo    the proximate letter (case sensitive!)
echo   -an asterisk means that the entire rest of the string will be
echo    saved in the next FOR variable (including delimiters)
pause>nul
echo(
set "string=#1#$2# #4$5"
set string
echo(
echo for /f %%%%a in ("%string%") do echo "%%%%a"
for /f %%a in ("%string%") do echo "%%a"
pause>nul
echo(
echo for /f "tokens=1,2" %%%%a in ("%string%") do (echo "%%%%a" ^&echo "%%%%b")
for /f "tokens=1,2" %%a in ("%string%") do (echo "%%a" &echo "%%b")
pause>nul
echo(
echo for /f "tokens=2" %%%%a in ("%string%") do echo "%%%%a"
for /f "tokens=2" %%a in ("%string%") do (echo "%%a")
pause>nul
echo(
echo for /f "delims=#" %%%%a in ("%string%") do echo "%%%%a"
for /f "delims=#" %%a in ("%string%") do echo "%%a"
pause>nul
echo(
echo for /f "tokens=2 delims=#" %%%%a in ("%string%") do echo "%%%%a"
for /f "tokens=2 delims=#" %%a in ("%string%") do echo "%%a"
pause>nul
echo(
echo for /f "tokens=1* delims=#" %%%%a in ("%string%") do (echo "%%%%a" ^&echo "%%%%b")
for /f "tokens=1* delims=#" %%a in ("%string%") do (echo "%%a" &echo "%%b")
pause>nul
echo(
echo for /f "tokens=1-4 delims=#" %%%%a in ("%string%") do (echo "%%%%a" ^&echo "%%%%b" ^&echo "%%%%c" ^&echo "%%%%d")
for /f "tokens=1-4 delims=#" %%a in ("%string%") do (echo "%%a" &echo "%%b" &echo "%%c" &echo "%%d")
pause>nul
echo(
echo for /f "tokens=1-5 delims=#$" %%%%a in ("%string%") do (echo "%%%%a" ^&echo "%%%%b" ^&echo "%%%%c" ^&echo "%%%%d" ^&echo "%%%%e")
for /f "tokens=1-5 delims=#$" %%a in ("%string%") do (echo "%%a" &echo "%%b" &echo "%%c" &echo "%%d" &echo "%%e")
pause>nul
echo(
echo try more by yourself
pause>nul

Regards
aGerman

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: PLEASE HELP ME WITH "FOR" (I need to know!)

#4 Post by Rileyh » 22 Oct 2011 23:33

@aGerman,
Thanks for the post batch file, it makes more sense now.
Could you post a reply to my REMing problem please (not meaning to sound pushy, but it has been there for ages and no one has replied :))


Thanks again,
Rileyh

Post Reply