Page 1 of 1

Saving current path to a var (problem with pipe)

Posted: 09 Mar 2008 13:06
by Chase
I started writing Batch scripts just now, and they already prove very useful. But right now there's something I don't get. Using the | character, you use a pipe to send the output of one program to the input of another. Very un-Windows-like, at least. This works perfectly:

Code: Select all

echo 03-09-08 | date

the date command expects a new date in this format, and that's what i supply.
Now I want to save the current directory to a variable, but somehow this doesn't work:

Code: Select all

cd | set /p folder=

"set /p [VARNAME]=" prompts for a value to assign to VARNAME, and I'm giving it the output of cd. It refuses to work.
For some strange reason, this works:

Code: Select all

cd > tmp
set /p folder= < tmp

Thanks for any hints, I'm somewhat in the dark here

Posted: 09 Mar 2008 21:22
by DosItHelp
Chase,

Interesting observation.
In your case you could simply use the environment variable cd like this:

Code: Select all

set folder=%cd%


To parse the output of a command into a variable you would typically use the FOR /F command.

Code: Select all

set "folder="
for /f "tokens=*" %%A in ('cd') do set "folder=%%A"
echo.folder is '%folder%'

However why piping '|' and file redirection '<' behave different is indeed strange...

Posted: 16 Mar 2008 06:05
by Chase
Thanks for the info, and sorry for the delay - haven't been around in a while :)

Unrelated, but I didn't want to open a thread for it: Do you guys know of some overview of variable flags? like %~1 or %%dpn~i.. i keep seeing these, but can't find anything to look it up :?

Posted: 18 Mar 2008 23:44
by DosItHelp
Chase,

Click here and scroll down a little bit:
http://www.dostips.com/DosCommandRef.htm#CALL

Hope this helps ;)