1

I'm writing a simple batch script on Windows 7 to add some some environmental file paths. Trouble is, my set path command is failing because of spaces in the existing path:

SET PATH=C:\oracle\ora92\bin;C:\Program Files\Oracle\jre\1.3.1\bin;%Path%

This fails with '\ Microsoft cannot be found' which refers I think to this in the existing %path% variable :-

c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\

Is there a simple solution to this?

Update: Turns out the issue lies with a surrounding IF statement.This script throw up the mentioned error message:

IF "TEST" == "TEST" (
  ECHO Using TEST settings on %COMPUTERNAME%
  SET PATH="C:\oracle\ora92\bin;C:\Program Files\Oracle\jre\1.3.1\bin;%Path%"
  SET GDRIVE= (removed path)
) 

But if I remove the IF statement, it works fine:

ECHO Using TEST settings on %COMPUTERNAME%
SET PATH="C:\oracle\ora92\bin;C:\Program Files\Oracle\jre\1.3.1\bin;%Path%"
SET GDRIVE= (removed path)

Does anyone know why an IF statement is causing the SET PATH to fail?

user3046742
  • 235
  • 1
  • 11
  • I had no problems doing the same on Windows 8.1 and I think I did the same already on Windows 7 ?!? Perhaps playing around with quotation marks will help: SET PATH="C:\Oracle\;...%PATH% – Marged Jun 09 '15 at 19:32
  • Remove the extra n in Bin. Paths don't need spaces quoted as they are delimited on semicolon not space. –  Jun 09 '15 at 19:59
  • It turns out the problem is with the surrounding IF block surrounding the SET Path statement. If I remove the IF statement, the batch runs fine. I can't understand why though. – user3046742 Jun 10 '15 at 11:28
  • The real problem is not the space, but the fact that the IF block uses parenthesis, which cmd.exe sees as being closed by `Program Files (x86)`. rojo's answer gives the 2 solutions. – mivk Jul 08 '16 at 15:14

2 Answers2

7

To answer your question directly, the syntax for avoiding evaluation of parentheses and other special characters while setting a variable is set "var=value". But set is the wrong command to use here. What you should do is

setlocal enabledelayedexpansion
path C:\oracle\ora92\bin;C:\Program Files\Oracle\jre\1.3.1\bin;!PATH!

Delayed expansion will prevent your PATH variable's contents from being evaluated prematurely. Type path /? from a cmd console for more information on the path command.

If you want your path additions to be persistent, see the :append_path function in the example script in this answer.

Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
0

Using a GOTO command will allow you to move the PATH assignment outside of the IF statement. This eliminates the issues caused by having the right parenthesis in the existing PATH variable.

IF NOT "TEST" == "TEST" (
    GOTO skiplabel
)

ECHO Using TEST settings on %COMPUTERNAME%
SET PATH="C:\oracle\ora92\bin;C:\Program Files\Oracle\jre\1.3.1\bin;%Path%"
SET GDRIVE= (removed path)

:skiplabel
ECHO Do other stuff as desired

R Schultz
  • 486
  • 5
  • 10