Solved For /f loop with spaces in a text

August 19, 2012 at 10:59:46
Specs: Windows XP
hii professionals :)
i am in need to your help in scripting a batch file to capture a certain text or file name from a .txt file which contains spaces.
i am using this one that works very good with file names not containing spaces :
for /F "usebackq tokens=5*" %%b in ("%tmp%\exe\exetmp.txt") do (bla bla bla...)
and the text file contains:
07/10/2010 08:59 PM 584,179 L4D2 Control Panel.exe
all i wanna to do to capture "L4D2 Control Panel.exe" only so i that possible??
thanks in advance and sorry for my bad english :)

See More: For /f loop with spaces in a text

Report •

✔ Best Answer
August 19, 2012 at 21:13:13
assuming you want "c" not "b":

for /f "tokens=5* delims=, " %%b in ("%tmp%\exe\exetmp.txt") do "%%c"
:end
might work.



#1
August 19, 2012 at 12:44:58
what you posted is pretty much it, just "shift" to %%c as your target, add comma to the delimiters:
for /f "tokens=5* delims=, " %%b in ("%tmp%\exe\exetmp.txt") do echo target aquired: %%c

Report •

#2
August 19, 2012 at 17:53:38
thanks alot for your precious reply but i would ask a question how %%c work and i did not put it instead of %%b in :

for /f "tokens=5* delims=, " %%b in ("%tmp%\exe\exetmp.txt") do (bla bla..)
i mean that just %%b will work not %%c can you explain more?
thanks in advance :)


Report •

#3
August 19, 2012 at 18:34:36
the tokens are assigned in sequence to each variable (%%a, %%b etc, but beginning with the one you put in the statement, in this case, %%b).
Since you started with "b", then "c" is the one that will catch the "*" (since we only specified to capture two tokens: #5, and 6-onwards implied by the asterisk after 5).
It is confusing. I always have to mess around before I get the tokens and delims to line up where I intend. The comma, in delims split off one more token, leaving only one more, "lda" to fill out the 5 required before running the rest into var %%c with no further parsing. Hope this helps. I hardly ever get it right the first time.

Report •

Related Solutions

#4
August 19, 2012 at 18:54:39
so if i wanna do something like that :
for /f "tokens=5* delims=, " %%b in ("%tmp%\exe\exetmp.txt") do start %%b
what certainly i should edit??
sorry again :)

Report •

#5
August 19, 2012 at 21:13:13
✔ Best Answer
assuming you want "c" not "b":

for /f "tokens=5* delims=, " %%b in ("%tmp%\exe\exetmp.txt") do "%%c"
:end
might work.


Report •

#6
August 19, 2012 at 22:27:00
thx alot it works like this :
for /f "tokens=5* delims=, " %%b in ("%tmp%\exe\exetmp.txt") do "%%b %%c"
coz %%b will complete the whole word of "L4D2 Control Panel.exe" and just %%c had a token of "panel.exe"
but i have a small problem now the name of the file now look like "L4D2 Control Panel.exe.exe" with 2 .exe how can i make it remove one of .exe in the name?
thanks again nbrane :)

Report •

#7
August 20, 2012 at 18:06:52
You probably should post your current script and maybe also another sample from the text file. Things don't match up: my script ran ok with the data you put in original post:
07/10/2010 08:59 PM 584,179 L4D2 Control Panel.exe
It got:
L4D2 Control Panel.exe
as target aquired. Also, somewhere along the line, we dropped out the "usebackq", which probably needs to be there due to %tmp% being a component of the filename. (usebackq is only needed to handle spaces in file/path names. double quotes around the "core" means "literal string" if usebackq is not applied, then without it you would (or should) get something like:
c:\docume~1\Username\Locals~1\temp\exe\exetmp.txt
all in variable %%b
My test-set worked with usebackq, starting with %%b, target aquired from %%c,
with no double ".exe", so something has drifted off the beam somewhere along the line. It is better to resolve any discrepancies, otherwise they are "floating mines in the harbor" waiting to sink your ship.

Report •

#8
August 20, 2012 at 18:38:10
ok thanks alot nbrane i knew what's the problem and now works so good really thank you....you have teached me something so useful about for /f :)

Report •

Ask Question