Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use setx command in a windows batch file

I am trying to create a windows batch file to automatically set the environment variable to use python 2.4 or python 3.3.

Both python 2.4 and 3.3 are installed on my system. Here is my code:

::To toggle between Python24 and Python 33
@echo on
if (%PYTHONHOME:~-2%) == "24" (setx PYTHONHOME "C:\Python33" && setx PATH %PATH:Python24=Python33% ) else (setx PYTHONHOME "C:\Python24" && setx PATH %PATH:Python33=Python24% )
pause

To start with I have PYTHONHOME set to C:\Python24

But the above script gives the following error:

SUCCESS: Specified value was saved.
ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.

My PYTHONHOME still points to python 24 and nothing is changed. The setx command does not change the environment variable. What is causing this error?

like image 368
Manmohan Singh Avatar asked Jan 21 '13 16:01

Manmohan Singh


People also ask

What does Setx do in Windows?

You can use the setx command to set values for user and system environment variables from one of three sources (modes): Command Line Mode, Registry Mode, or File Mode. Setx writes variables to the master environment in the registry.

How do I set an environment variable in a batch file?

When creating batch files, you can use set to create variables, and then use them in the same way that you would use the numbered variables %0 through %9. You can also use the variables %0 through %9 as input for set. If you call a variable value from a batch file, enclose the value with percent signs (%).

How do I echo environment variables in Windows?

To reference a variable in Windows, use %varname% (with prefix and suffix of '%' ). For example, you can use the echo command to print the value of a variable in the form " echo %varname% ".

How can I see environment variables in CMD?

To Check if an Environment Variable ExistsSelect Start > All Programs > Accessories > Command Prompt. In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable.


1 Answers

The Windows command line error:

ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.

Summary:

You are using a setx command and assigning it multiple tokens when only one is allowed.

How to reproduce this error on Windows:

Open a windows cmd terminal and enter these commands. This throws the error:

C:\Users\Charity>setx FANCYPANTS string with spaces

ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.

Do the same command, but quote your string like this:

C:\Users\Charity>setx FANCYPANTS "string with spaces quoted"
SUCCESS: Specified value was saved.
C:\Users\Charity>

The variable was set, restart the cmd terminal here to load changes.

C:\Users\Charity>echo %FANCYPANTS%
string with spaces quoted

The environment variable is saved. Now delete it.

C:\Users\Charity>setx FANCYPANTS ""
SUCCESS: Specified value was saved.

restart the cmd terminal here to load changes. Print contents again.

C:\Users\Charity>echo %FANCYPANTS%
%FANCYPANTS%

the variable FANCYPANTS was deleted and no longer exists.

like image 174
Eric Leschinski Avatar answered Sep 24 '22 01:09

Eric Leschinski