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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch For /F delims=, bug?

Reply to Message Icon

Name: JRell
Date: January 8, 2008 at 11:28:28 Pacific
OS: Win XP Pro
CPU/Ram: intel pentium 2gb
Product: 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!



Sponsored Link
Ads by Google

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

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!!!



0

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

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?


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch For /F delims=, bug?

Another Edit file FOR /F www.computing.net/answers/programming/another-edit-file-for-f/19930.html

batch for /f not working right? www.computing.net/answers/programming/batch-for-f-not-working-right/17514.html

batch file: error SETting in FOR /f www.computing.net/answers/programming/batch-file-error-setting-in-for-f/16520.html