Saving current path to a var (problem with pipe)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Chase
Posts: 4
Joined: 09 Mar 2008 12:27

Saving current path to a var (problem with pipe)

#1 Post by Chase » 09 Mar 2008 13:06

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

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 09 Mar 2008 21:22

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

Chase
Posts: 4
Joined: 09 Mar 2008 12:27

#3 Post by Chase » 16 Mar 2008 06:05

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 :?

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#4 Post by DosItHelp » 18 Mar 2008 23:44

Chase,

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

Hope this helps ;)

Post Reply