Brackets in Windows cmd-script variable values ​​not allowed?

Why do you get the following Windows 7..cmd script command:

set SUN_JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_17 if 3==3 ( set JAVA_HOME=%SUN_JAVA_HOME% ) echo ready 

The following error message instead of printing "ready"

 \Java\jdk1.6.0_17 was unexpected at this time. 

The error message will disappear if I delete "(x86)" in the path name.

+4
source share
5 answers

The problem is grouping the brackets after the if 3==3 .

When parsing the set JAVA_HOME=%SUN_JAVA_HOME% interpreter immediately replaces the %SUN_JAVA_HOME% variable and causes the closing parenthesis to match early in (386) .

This can be avoided if you enable the deferred extension and replace %SUN_JAVA_HOME% with !SUN_JAVA_HOME! :

 setlocal enabledelayedexpansion set SUN_JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_17 if 3==3 ( set JAVA_HOME=!SUN_JAVA_HOME! ) echo ready 
0
source

at the command prompt, enter the following commands

 C: CD\ dir /ogen /x 

This will show you an 8 digit name for Program Files (x86)

Use this name (possibly "Progra ~ 2" )

+2
source

you need to enclose the set command with double quotes

replace

 set SUN_JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_17 

by

 set SUN_JAVA_HOME="C:\Program Files (x86)\Java\jdk1.6.0_17" 

since there is a space in the way

+1
source

I already wrote about this some time ago (a bit outdated by now).

Alternatively, if you need a grouping of commands, use a routine:

 if 3==3 call :foo ... goto :eof :foo ... goto :eof 
0
source

The previous answer is ok. I just want to clarify this with a simple example. About discovering the Program Files directory for a 32-bit application on x86 and x64 systems. There is a similar problem with "(x86)".

 IF DEFINED ProgramFiles(x86) (GOTO x64) ELSE (GOTO x86) :x64 SET AppDir=%ProgramFiles(x86)%\SomeFolder GOTO next :x86 SET AppDir=%ProgramFiles%\SomeFolder :next ECHO %AppDir% 
0
source

Source: https://habr.com/ru/post/1303569/


All Articles