I have a USB flash drive with portable installs of Dropbox and emacs. One of the functions I want to run in emacs needs to have sha1sum.exe in the path, so I also put coreutils on the USB. I am writing a batch file which:

  1. Puts the path to sha1sum.exe in PATH. (while I'm at it, also the path to emacs and Dropbox)
  2. Runs Dropbox in a separate process.
  3. Runs emacs.

Here is what I have:

set PATH="%PATH%;%~d0\pathtocoreutils\bin;%~d0\pathtodropbox;%~d0\pathtoemacs\bin"
echo %PATH%
timeout 10
start DropboxPortableAHK.exe
timeout 10

When I run it in a new command window, the set and echo commands give the expected results, but everything else gives errors indicating that the files are not on the path (including timeout, which was added for debugging and is not in any of the new PATH entries):

'timeout' is not recognized as in internal or external command,
executable program or batch file.
The system cannot find the file DropboxPortableAHK.exe.
'timeout' is not recognized as in internal or external command,
executable program or batch file.
'runemacs.exe' is not recognized as in internal or external command,
executable program or batch file.

If I run it a second time in the same command window, then it works fine. Executing the same commands individually from the command line also works. Opening a new terminal will also cause the batch file to fail once. The behavior I'd like is to be able to run the batch file by clicking on it, but in that case, there is no ability to run it a second time in the same instance of cmd.exe.

My first thought was that execution proceeded to the other commands before set PATH= was complete, and PATH was completely inaccessible. That's why I added the timeouts. However, the fact that echo %PATH% returns the correct updated value suggests that this isn't really the issue. Also, the failed start opens a message box, and execution does not pass to the last two lines until it is done, yet these lines still fail.

Everything I've been able to turn up about setting PATH in a batch file is for people trying to get the change to persist system-wide. The answers all suggest that the normal behavior is for set PATH= to work within the same terminal instance, including later in the same batch file.

I'm testing on Windows 7 Professional sp1 without admin access.

What's going on here, and how can I fix it?

2 Answers 2

set "PATH=%PATH%;%~d0\pathtocoreutils\bin;%~d0\pathtodropbox;%~d0\pathtoemacs\bin"

What you should not do is to include the quotes in the value, but you can (and in same cases you should) quote the assignment.


I found my answer: There should be no quotes in the set PATH= line. It worked the second time in the same window because then there were an even number of quotes at the beginning.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.