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.