Windows – PATH variable and quotation marks (windows)

environment-variablespathwindowswindows-server-2008

I am having a weird problem with the PATH variable under windows:

My application is in a folder c:\app\bin and the DLLs for this application are in the c:\app\runtime folder. To run my program I modify the PATH variable with a *.bat file usually with the following script:

set PATH="c:\app\bin";"c:\app\runtime";%PATH%

This will bring the executables and the DLLs on the path. However, on one of my Windows Server 2008 R2 systems this does not work. That means, if I execute the above command in a command window, I can start the exe file from c:\app\bin, but the application complains immediately that it cannot find some dll files required ("The program can't start because ….dll is missing from your computer …"). These dll files should be in c:\app\runtime.

I experimented a little bit and it points out that there are three workarounds:

  1. Modify the PATH variable permanently using the System Properties dialog
  2. Omit the quotation marks in the above command for the path of the DLL files, e.g. set PATH="c:\app\bin";c:\app\runtime;%PATH%
  3. Copying the DLL files to the directory where the exe is located

The weird part about solution 2 is, that it does not change anyhing if I add quotation marks to the first path, or if I change the order of the paths.

Has someone a clue why my original script does not work? I need to get it run, because it is created automatically by a program and I cannot change the application that generates the bat file.

Best Answer

The PATH variable doesn't ordinarily contain quotes; it uses semicolons as its delimiter. For example, here is my system's PATH definition, which includes folders with spaces:

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft Windows Performance Toolkit\

It seems that Windows can execute programs with "quoted" paths, but the DLL search routine can't handle them.

Ideally you should use:

set PATH=c:\app\bin;c:\app\runtime;%PATH%

Another workaround might be to launch the program from c:\app\runtime, eg:

cd /d C:\app\runtime
..\bin\app.exe

That may not even require modifying the PATH variable.