quotes and spaces in directory paths

I dislike dealing with spaces in directory names.  It's painful typing things like C:\DOCUME~1\TALLARICO\ or C:\PROGRA~1 and remembering the nuances of 8.3, double-double quotes and such.  Moreover I'm sick of having to tweak my scripts to work on a new server and troubleshoot errors like filename not found.

In working with Visual Studio recently I noticed that the deployment .bat's that come with VSS 2008 contained a ridiculously cool command:  pushd.

Open a cmd prompt on a Windows box and you may start at your Home Directory or %WINDIR%, or any number of places depending on who you are and what you've done with your PC.  Other users may have tweaked their profiles to start somewhere else.  Leverage something like a remote process starter and you'll start at %WINDIR%\...

Up until my discovery of pushd I was prefacing my .bat scripts with things like cd.. or cd \ or even specifying the entire UNC paths to the files referenced in my scripts.  Just to ensure the commands I ran in the script wouldn't error out because they weren't in the directory the script was executing from.

pushd takes a single argument, the path to which you want to make the context of the current directory.

If you go to Start > Run and type "notepad" it pops open.  Why?  Because its location is within the %PATH% environmental variable - C:\WINDOWS\notepad.exe.  If you specify a filename like notepad.exe your %PATH% is an exhaustive listing of where Windows will look for that file before turning out the dreaded "...is not recognized as an internal or external command" error we all know and love.

Open a cmd prompt and type echo %PATH% - whoomph.  There it is:  the extent of the directories to whose files you won't have to specify the complete paths to run.

When you write scripts and schedule them to run on servers, or run remotely, or under different user accounts the directory they start executing from could be anywhere.  There's a lot of ways around this and, frankly, they are all lacking:

  • You could manipulate %CD% - the current directory variable
  • Specifying full UNC paths - \\ryanboyer.net\miserable\path\to\file.exe
  • Using the tilde, as noted above, - C:\DOCUME~1\TALLARICO\file.exe
  • Leverage cd as in cd \ (for root directory), or cd .. (for parent directory)

With pushd you can open a command prompt and specify the local or remote directory that you want to be the current directory for the context of that session.  If it's a UNC path pushd will map your next available drive letter

Think about that.  Your next available drive letter.  So no more managing drive letters for thousands of users - keeping track of the limited 26 alphabetic characters in, say, logon scripts.  You can simply specify the next available drive letter for the context of that user.

Clean up after you're done, of course.  Use pushd's counterpart, popd (named for "popping" it off the stack, presumably).  This cleans up the drive letter and returns you to the original directory before pushd was executed.

The best part?  No more spaces.  Say I want to execute a script that runs these executables in short order:

\\Server1\Long Path With Spaces\First.exe

\\Server1\Long Path With Spaces\Second.exe

I would simply create a .bat like this: