for /f question??

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

for /f question??

#1 Post by Dos_Probie » 19 Jun 2013 11:00

Got the following code snippet below with 2 "for /f" commands and both work which one should I use or does it even matter? I read where delims takes care of file spaces..Thanks DP

Code: Select all

@echo off&color a
@setlocal enableextensions
@cd /d "%~dp0"

:: with tokens and delims
for /f "tokens=1 delims=" %%a in ('dir/b "%~dp0msu\*.msu"') do (
:: without
for /f %%a in ('dir/b "%~dp0msu\*.msu"') do (

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

Re: for /f question??

#2 Post by Squashman » 19 Jun 2013 11:04

That is a pretty open ended question. You would use the one for what matches your input and how you need your data output.

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: for /f question??

#3 Post by Dos_Probie » 19 Jun 2013 12:08

Thanks but again they Both provide the same output (Install my .msu update/s) just trying to understand why or if I would even need the tokens,delims if the other command will Do the same and is more concise etc etc.

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

Re: for /f question??

#4 Post by Squashman » 19 Jun 2013 12:11

They work the same because your data has none of the default delimiters.

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

Re: for /f question??

#5 Post by aGerman » 19 Jun 2013 13:34

@Dos_Probie
Check this out and read the help message about FOR /F:

Code: Select all

@echo off &setlocal
set "s1=word"
set "s2=This is a sentence with spaces."
set "s3= This is a sentence with prepended space."

for /f %%i in ("%s1%") do echo "%%i"
for /f %%i in ("%s2%") do echo "%%i"
for /f %%i in ("%s3%") do echo "%%i"
echo(
for /f "tokens=*" %%i in ("%s1%") do echo "%%i"
for /f "tokens=*" %%i in ("%s2%") do echo "%%i"
for /f "tokens=*" %%i in ("%s3%") do echo "%%i"
echo(
for /f "delims=" %%i in ("%s1%") do echo "%%i"
for /f "delims=" %%i in ("%s2%") do echo "%%i"
for /f "delims=" %%i in ("%s3%") do echo "%%i"
echo(
pause


To make sure that you would get always the whole output of your command I would prefer "delims=" if I were you. It's your decision though.

Regards
aGerman

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: for /f question??

#6 Post by Aacini » 19 Jun 2013 15:16

Perhaps these notes may be useful for you:

- The default value for tokens is "tokens=1", so you may always eliminate this value and get the same result.

- The default value for delims are spaces and tab's. When someone write "delims= " the real meaning is "restrict delimiters to just spaces and don't separate the value at TAB's". I think nobody really wants to mean this, so using "delims= " is one of the common bad Batch practices.

- This way, a FOR /F with no options is equivalent to FOR /F "tokens=1 delims= and tab's"

- When processing complete lines, "tokens=*" eliminate spaces before the first token, but "delims=" preserve the original line.

I hope it helps!

Antonio

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: for /f question??

#7 Post by Dos_Probie » 19 Jun 2013 16:15

Thanks aGerman and Aacini for the examples and explanation this helps a lot! DP :D

Post Reply