Page 1 of 1
parsing the %CD% variable
Posted: 20 May 2012 09:58
by timbertuck
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?
Re: parsing the %CD% variable
Posted: 20 May 2012 10:32
by aGerman
Try
Code: Select all
for %%i in ("%cd:\=" "%") do if %%i neq "" echo %%~i
Regards
aGerman
Re: parsing the %CD% variable
Posted: 20 May 2012 10:59
by timbertuck
aGerman wrote:Try
Code: Select all
for %%i in ("%cd:\=" "%") do if %%i neq "" echo %%~i
Regards
aGerman
brilliant! many thanks aGerman
Re: parsing the %CD% variable
Posted: 20 May 2012 19:49
by Fawers
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.
Re: parsing the %CD% variable
Posted: 20 May 2012 20:56
by phillid
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
Re: parsing the %CD% variable
Posted: 20 May 2012 21:14
by foxidrive
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.
it is similar to this usage of the for command:
Code: Select all
@echo off
for %%a in (one two three) echo %%a
Re: parsing the %CD% variable
Posted: 20 May 2012 21:16
by Fawers
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:
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:
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.
Re: parsing the %CD% variable
Posted: 20 May 2012 21:49
by foxidrive
That's a good description fawers.
Re: parsing the %CD% variable
Posted: 20 May 2012 22:30
by phillid
Awesome!
Thanks foxidrive and Fawers!
Phillid
Re: parsing the %CD% variable
Posted: 21 May 2012 07:56
by Fawers
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.
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.
Re: parsing the %CD% variable
Posted: 21 May 2012 12:26
by aGerman
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
)
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
Re: parsing the %CD% variable
Posted: 22 May 2012 15:42
by timbertuck
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.