parsing the %CD% variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

parsing the %CD% variable

#1 Post by timbertuck » 20 May 2012 09:58

how can i parse the current path into tokens when i don't know how many tokens there are. right now, i have this code but not pretty at all:

Code: Select all

@echo off
for /f "tokens=1-13* delims=\" %%a in ("%cd%") do (
   @echo %%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m
)


there must be a more elegant way of doing this. ideas?

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

Re: parsing the %CD% variable

#2 Post by aGerman » 20 May 2012 10:32

Try

Code: Select all

for %%i in ("%cd:\=" "%") do if %%i neq "" echo %%~i

Regards
aGerman

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: parsing the %CD% variable

#3 Post by timbertuck » 20 May 2012 10:59

aGerman wrote:Try

Code: Select all

for %%i in ("%cd:\=" "%") do if %%i neq "" echo %%~i

Regards
aGerman


brilliant! many thanks aGerman

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: parsing the %CD% variable

#4 Post by Fawers » 20 May 2012 19:49

aGerman wrote:Try

Code: Select all

for %%i in ("%cd:\=" "%") do if %%i neq "" echo %%~i

Regards
aGerman


Damn, aGerman. That was genius! I would never had thought of that.
As said before, brilliant.

phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

Re: parsing the %CD% variable

#5 Post by phillid » 20 May 2012 20:56

I'm not too savvy with regards to the for command, could someone please spell this one out and explain what each part does? I don't find for /? much help..

Thanks
Phillid

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: parsing the %CD% variable

#6 Post by foxidrive » 20 May 2012 21:14

Open a cmd prompt in a deep nested folder and use this command (which is inside the for command above).
That should show you how the for command is used in this case - because this is what is replaced in the parentheses when it runs.

Code: Select all

echo "%cd:\=" "%"



it is similar to this usage of the for command:

Code: Select all

@echo off
for %%a in (one two three) echo %%a

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: parsing the %CD% variable

#7 Post by Fawers » 20 May 2012 21:16

This "version" of the FOR loop (without any extra option like /L, /F) will output text separately; in this case, the default delimiter are blank spaces.
For instance, if you write the code like this:

Code: Select all

for %%n in (My name is john) do echo %%n

it will output 4 different strings:

Code: Select all

My
name
is
john


But this is changed if you enclose the text in quotes.

Code: Select all

for %%n in ("My name is john") do echo %%n

"My name is john"

And removing the quotes on the variable:

Code: Select all

for %%n in ("My name is john") do echo %%~n

My name is john


What aGerman did is, he removed the back slashes in "%cd%" and replaced them with a space enclosed in double quotes.
Test it:

Code: Select all

echo %cd:\=" "%

Assuming the current directory is C:\Documents and Settings\All Users\Desktop, it would output
C:" "Documents and Settings" "All Users" "Desktop

"But what about the first and last quotes?"

Try it with "%cd:\=" "%".
New result:
"C:" "Documents and Settings" "All Users" "Desktop"

This means that FOR will output 4 different strings, in which the last one is Desktop.

Code: Select all

::Current directory: C:\Documents and Settings\All Users\Desktop
for %%n in ("%cd:\=" "%") do echo %%~n

C:
Documents and Settings
All Users
Desktop


I don't know, though, if this was clear enough.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: parsing the %CD% variable

#8 Post by foxidrive » 20 May 2012 21:49

That's a good description fawers.

phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

Re: parsing the %CD% variable

#9 Post by phillid » 20 May 2012 22:30

Awesome! :D

Thanks foxidrive and Fawers!

Phillid

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: parsing the %CD% variable

#10 Post by Fawers » 21 May 2012 07:56

I appreciate the feedback, as it looks like I didn't get sidetracked.

foxidrive wrote:Open a cmd prompt in a deep nested folder and use this command (which is inside the for command above).
That should show you how the for command is used in this case - because this is what is replaced in the parentheses when it runs.

Code: Select all

echo "%cd:\=" "%"




it is similar to this usage of the for command:

Code: Select all

@echo off
for %%a in (one two three) echo %%a


This is the essence of almost everything I said, Phillid.

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

Re: parsing the %CD% variable

#11 Post by aGerman » 21 May 2012 12:26

Fawers wrote:Damn, aGerman. That was genius! I would never had thought of that.
As said before, brilliant.

Thanks but I'm sure originally it wasn't my idea.

Thank you for your explanation. I always appreciate it (since I'm sometimes too lazy, sorry :wink:)
In case somebody was wondering why I included the IF statement:
It's only if %cd% contains the drive root - it includes a trailing back slash.
C:\ -> "%cd:\=" "%" -> "C:" ""

Regards
aGerman

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: parsing the %CD% variable

#12 Post by timbertuck » 22 May 2012 15:42

great explanation Fawers, im sure that will help out others stumbling on to this path... and thanks again aGerman for passing along that great code. glad that it has perked interest.

Post Reply