Invalid syntax with setx - windows

I used the setx command to set OGRE_HOME:
setx OGRE_HOME D:\Program Files\OgreSDK
Now I need to change to value of OGRE_HOME.
How can I search all the values I have set?
If I run the command again, it shows that:
ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).

Your path to the Ogre SDK has a space character in it, which is interpreted as a delimiter to another argument. Surround your path with " to keep it as one single argument to setx:
setx OGRE_HOME "D:\Program Files\OgreSDK"
To see the current value of the OGRE_HOME environment variable:
echo %OGRE_HOME%
You may have to open a new command prompt shell to see the value if you set it and are then trying to immediately see it's value.
To see all currently set environment variables, simply run:
set
To show only environment variables that have a certain prefix (so FOO would show FOOBAR and FOOBAZ), put that prefix after set:
set PREFIX
Alternatively, you can use the GUI to edit environment variables (assuming Windows 7 here).
Right-click Computer, choose Properties
Click Advanced system settings in the left pane
Make sure you're on the Advanced tab in the pop-up dialog
Click Environment Variables... at the bottom
A dialog will pop up with your user-specific environment variables as well as your system-wide environment variables. Select a value and use the New/Edit/Delete buttons to interact with them.

Command Prompt is giving you that error because you forgot the quotation marks. You should’ve typed:
setx OGRE_HOME "D:\Program Files\OgreSDK"
To see all the values you’ve already set, enter either:
reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
OR
reg query HKEY_CURRENT_USER\Environment

setx and pretty much all windows command line commands are sensitive to certain special characters. Among them the space character but there's also the quote which is used to delimit an entry.
As #ajp15243 already said, you can deal with the space by locking off the path{s) between two quotations. But what if you have paths and those path already have quotations because they carry a space? Here's an example:
MY_PATHS="c:\Program Files\path1";"c:\Program Files(x86)\Path2"
In this case, you would have to put escape characters for those inner quotation marks when you use setx or it will get confused and give the error you listed. Eg:
setx -m MY_PATHS "\"c:\Program Files\path1\";\"c:\Program Files(x86)\Path2\""

As an addendum to #ajp15243's answer. If you are doing the same with PowerShell rather than the command prompt or batch file, you'll need to call SETX with a leading escaped double-quote character, as in:
$my_path = "%PROGRAMFILES%\MySQL\MySQL Server 5.7\bin\"
$hkcu_path = (Get-ItemProperty hkcu:\Environment).PATH + ";" + $my_path
SETX PATH "`"$hkcu_path" # note the leading escaped quote
However doing so, may result in adding a trailing double quote in the value of hkcu:\Environment\PATH, so you may need to do this too:
$dirty_path = (get-itemproperty hkcu:\Environment).PATH
$clean_path = $dirty_path -replace '"',''
SETX PATH $clean_path

Related

Print out environment variable

I created an environment variable as follows:
setx HTTPS_PROXY "website"
And when I try to print it out:
echo %HTTPS_PROXY%
%HTTPS_PROXY%
PetSerAl's comment summarizes the reason from setx /?:
NOTE: 1) SETX writes variables to the master environment in the registry.
2) On a local system, variables created or modified by this tool
will be available in future command windows but not in the
current CMD.exe command window.
…
Solution: you need to use SET command, e.g. as follows.
set "HTTPS_PROXY=website"
setx HTTPS_PROXY "%HTTPS_PROXY%"
Note that (supposedly complex) website string is typed only once in above code snippet (to avoid a keying mistake) in so far that %HTTPS_PROXY% surely matches data in registry:
reg query hkcu\environment /V HTTPS_PROXY

Are environment variables expanded when searching PATH?

This question expands on the comments on this deleted answer. I claimed that an unexpanded variable reference in the PATH would not be expanded when searching for an executable, but Ken said he didn't see the same behaviour that I did.
Note that the ordinary situation is subtly but critically different: subject to certain conditions, environment variables are automatically expanded when the PATH environment variable is built from the information in the registry. I'm talking about the case where, for one reason or another, this hasn't happened, so the actual environment block of the cmd.exe process contains a PATH which still has environment variable references in it.
Here is the code I built to test this behaviour:
md test1
echo echo hello! > test1\test1.cmd
set TESTPATH=%cd%\test1
set percent=%%
set PATH=c:\windows\system32;c:\windows;c:\windows\system32\Wbem;%percent%TESTPATH%percent%
set PATH
set TESTPATH
test1
cmd /c test1
start test1.cmd
and this is the result on my machine:
C:\working\testpath>test
C:\working\testpath>md test1
C:\working\testpath>echo echo hello! 1>test1\test1.cmd
C:\working\testpath>set TESTPATH=C:\working\testpath\test1
C:\working\testpath>set percent=%
C:\working\testpath>set PATH=c:\windows\system32;c:\windows;c:\windows\system32\
Wbem;%TESTPATH%
C:\working\testpath>set PATH
Path=c:\windows\system32;c:\windows;c:\windows\system32\Wbem;%TESTPATH%
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py;.pyw
C:\working\testpath>set TESTPATH
TESTPATH=C:\working\testpath\test1
C:\working\testpath>test1
'test1' is not recognized as an internal or external command,
operable program or batch file.
C:\working\testpath>cmd /c test1
'test1' is not recognized as an internal or external command,
operable program or batch file.
C:\working\testpath>start test1.cmd
The system cannot find the file test1.cmd.
What is the expected behaviour? Does it vary depending on the version of Windows and/or other factors?
There are two entirely different points of view in this question:
Q: How to expand the values of variables embedded in another one? This point is not specific to PATH variable, but works on anyone.
A: Enclose the names of the variables in exclamation marks and enable delayed expansion when you want to expand such values:
#echo off
setlocal EnableDelayedExpansion
set TESTPATH=%cd%\test1
set "PATH=c:\windows\system32;c:\windows;c:\windows\system32\Wbem;^!TESTPATH^!"
set PATH
echo PATH=%PATH%
Q: Does cmd.exe such delayed expansion when it use PATH to locate an executable file?
A: No. cmd.exe uses the values in PATH variable as they appear, with no further processing. Any special character that may appear in PATH, like percents or exclamation-marks, are taking literally.

pathnames with spaces in batch script

I have a problem with spaces in directory names in a batch script.
I store a base directory and then use that to make subdirectories and files, something like:
set basepath=c:\some\path
set logdir=%basepath%\log
set logfile=%logdir%\test.log
But the basepath on some servers have spaces in it. Earlier I used dir /x to get the shortened 8.3 names but I encountered one server where this doesn't work (apparently there is some setting to disable this, and I don't have privileges to turn it back on). So now I'm trying to figure this out. I need to concatenate filename/directories to basepath, which may have spaces in it. I tried using double quotes, but it didn't work.
At the command prompt, you can do things like cd "some path"\with\spaces using a combination of double quoted directories and non-double-quoted directories. But this doesn't work in a batch script.
Any suggestions?
set "basePath=c:\somewhere\over the rainbow"
set "logDir=%basePath%\logs"
set "logFile=%logDir%\kansas.log"
>> "%logFile%" echo This is a test
cd "%logDir%"
Don't insert quotes inside the variable values (unless it is necessary).
Use quotes surounding the set command to ensure no aditional spaces are stored in variables and to protect special characters.
Place quotes in the correct places in the final commands that make use of the variables.
Put double quotes around the environment variable only when you need to actually use it.
set basepath=c:\some\path with spaces
set logdir=%basepath%\log
xcopy *.log "%logdir%"
Then reference it as "%logdir%" and it will expand to "c:\some\path with spaces\log". This works because set puts everything after the = except for including trailing white-space into the environment variable.

Concatenate env variable value with string in command line

This is Windows 7 command prompt question.
Suppose I have environment variable which were set in a next way:
set FILE_SRC="D:\Users\me\Documents and Settings"
I would like to call form command line utility which will get one of directory files as argument:
fooUtil.exe %FILE_SRC%\fileName.txt
In this case shell fails to construct correct path string. Instead of it utility get next argument:
"D:\Users\me\Documents and Settings"\fileName.txt
What is a correct way? Again, I talk about prompt command line and not a batch file.
I make it so ...
set "FILE_SRC=D:\Users\me\Documents and Settings"
fooUtil.exe "%FILE_SRC%\fileName.txt"
This works also with special characters.
set "line=lines & edges = figures"
#echo "%line%"
Just skip the quote marks when setting the variable. The variable will be set to the value terminated by a newline, not space.

How is it possible to view windows environment variable without executing arguments within it?

Basically I would like to export my exact PATH variable to a file automatically. It contains things like %ANT_HOME%/bin and I would like to keep it that way. From what I could find, using both set and echo will execute that argument and give me the absolute path. Is there something I'm missing?
To get a copy of your PATH without expansion of environment variables you could save the following as "rawPath.vbs"...
Option Explicit
Dim wsh
Set wsh = CreateObject("Wscript.Shell")
Wscript.Echo wsh.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")
...and then issue the following command to pipe the output to a file
cscript -nologo rawPath.vbs > myPath.txt
Do you see %ANT_HOME% when you execute SET from the prompt?
If so,
>filename echo %path%
should export the path as desired.
If the PATH variable does not actually contain the "%" characters, then it's already been resolved. And remember, "%" is actually a legitimate (but annoying) filename character...
You CAN set a "%" character into an environment variable
set var=%%something%%
will set var to %something%
You need to escape the % good sir, example
>echo %path%
C:\windows\system32;C:\windows\system32\wbem
>echo ^%path^%
%path%

Resources