Computing.Net > Forums > Programming > Batch For /F delims=, bug?

Batch For /F delims=, bug?

Reply to Message Icon

Original Message
Name: JRell
Date: January 8, 2008 at 11:28:28 Pacific
Subject: Batch For /F delims=, bug?
OS: Win XP Pro
CPU/Ram: intel pentium 2gb
Model/Manufacturer: dell
Comment:

Well... my forehead is now a bloody pulp...

Try this batch file:

@echo off
set myvar=%ALLUSERSPROFILE%

echo String to parse:[%myvar%]
echo.
for /f "usebackq tokens=1-7* delims=\" %%a in (`echo %myvar%`) do call :prtpath %%a %%b %%c %%d %%e %%f %%g
goto EOF

:prtpath
echo Element 1 = %1
echo Element 2 = %2
echo Element 3 = %3
echo Element 4 = %4
echo Element 5 = %5
echo Element 6 = %6
echo Element 7 = %7

:EOF
---------------------
Here are the results:
String to parse:[C:\Documents and Settings\All Users]

Element 1 = C:
Element 2 = Documents
Element 3 = and
Element 4 = Settings
Element 5 = All
Element 6 = Users
Element 7 =

Notice that "...delims=\" and there is NO SPACE included there for a delimiter, however, it is tokenizing on \ as well as SPACE!!!!

Is there a way around this behaviour?

Thanks in advance!


Report Offensive Message For Removal


Response Number 1
Name: JRell
Date: January 8, 2008 at 12:20:48 Pacific
Reply: (edit)

duh....

the call to the :prtpath is the problem!

call gets expanded to:
:prtpath C: Documents and Settings All Users

which is correctly mapped to:
%1 = C:
%2 = Documents
%3 = and
%4 = Settings
%5 = All
%6 = Users

The trick is to double quote the strings that may contain spaces:
for /f "usebackq tokens=1-7* delims=\" %%a in (`echo %myvar%`) do call :prtpath "%%a" "%%b" "%%c" "%%d" "%%e" "%%f" "%%g"

oh well... maybe someone else will learn from my "pulpy" forehead!!!



Report Offensive Follow Up For Removal

Response Number 2
Name: cooltouch
Date: January 15, 2008 at 05:49:09 Pacific
Reply: (edit)

Jrell... I have actually been searching high and low for topics regarding this issue and also have been beating my head against a wall. I am attempting something very similar. Although what you discuss above adds insight, the result of your double quotes is that you not only get the folder with spaces as a single token, but that passed variable actually contains those same double quotes. Here is what I found when using your modified code:

String to parse:[C:\Documents and Settings\All Users]

Element 1 = "C:"
Element 2 = "Documents and Settings"
Element 3 = "All Users"
Element 4 = ""
Element 5 = ""
Element 6 = ""
Element 7 = ""

The quotes make it hard to reuse those values. My code is below:

@echo off
cls
setLocal EnableDelayedExpansion

dir /ad /b > dirlist.txt

call :readdirlist
goto eof

:readdirlist
REM FOR /F "tokens=1* delims=" %%G IN (dirlist.txt) DO (call :s_size %%G %%H)
REM FOR /F "tokens=1* delims= " %%G IN (dirlist.txt) DO (call :s_size %%G)
REM FOR /F "tokens=* delims=?" %%G IN (dirlist.txt) DO (call :s_size %%G)
FOR /F "tokens=* delims=" %%G IN (dirlist.txt) DO (call :s_size "%%G")
goto eof

:s_size
set folder=%1
echo %folder%
goto eof

:eof

I am attemting something similar, but cant for instance read from the file, 'Documents and Settings' as one token. After finding your post, I now can get one token but it has quotes on it. Did you come up with any additional findings during your quest that may help me?


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch For /F delims=, bug? 

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge