Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.
for /f "tokens=1 delims=." %%1 in ("%testfile%") do ( set filenam=%%1)echo %filenam%
I wouldn't make a habit of using numbers for the FOR loop variable especially if you have batch files that use cmd line input or you are calling another label or batch file.
for /f "delims=" %%1 in ("%testfile%") do set filenam=%%~n1
what am i doing wrong?
what is the difference between:tokens=1-5 and tokens=0,2 (the question is what is the difference between the dash and the comma)
C:\>for /f "tokens=1-5" %A in ("ant bee cat dog egg fire goes hat ink jet kit") do @echo %A %Bant beeC:\>for /f "tokens=1,5" %A in ("ant bee cat dog egg fire goes hat ink jet kit") do @echo %A %Bant eggC:\>for /f "tokens=1,2,6-7" %A in ("ant bee cat dog egg fire goes hat ink jet kit") do @echo %A %B %C %Dant bee fire goesC:\>for /f "tokens=1,2,6,8-9" %A in ("ant bee cat dog egg fire goes hat ink jet kit") do @echo %A %B %C %D %Eant bee fire hat inkC:\>for /f "tokens=1,9 delims=," %A in ("ant,bee,cat,dog,egg,fire,goes,hat,ink,jet,kit") do @echo %A %Bant inkC:\>for /f "tokens=11 delims=," %A in ("ant,bee,cat,dog,egg,fire,goes,hat,ink,jet,kit") do @echo %Akit
set testset=one two three four five six seven
for /f %a in ("%testset%") do ( echo %A = %a echo %B = %b echo %C = %c echo %D = %d echo %E = %e echo %F = %f echo %G = %g)>%A = one>%B = two>%C = three>%D = four>%E = five>%F = six>%G = seven
for /f "tokens=1,2" %a in ("%testset%") do echo %a %b>one two
for /f "tokens=2,4-6" %a in ("%testset%") do echo %a %b %c %d>two four five six
The default delimiters are the space and carriage returns.
delims=xxx - specifies a delimiter set. This replaces the default delimiter set of space and tab.
I see that Salmon Trout already beat me too it, but here is what I was working on while he was quicker on the draw this time.The tokens within a for statement are not well explained in the help text. I too had some questions on them and CH helped steer me in the right direction, so hopefully I can utilize what I was taught and pass it along.The only other caveat to add is the addition of the "*" at the end of the tokens statement. Using tokens=1* in the second code (for /f "tokens=1*" %a in ("%testset%") do echo %a %b) will actually give you the output of one two three four five six seven because it takes the first delimited token and assigns it to %a, but the "*" tells it to take the rest of the line and assign it to %b.Please let us know if you have any other questions on the for statement, and I'll try to locate the post that taught me about all of this.