How to use setx command in windows batch file

I am trying to create a Windows batch file to automatically set an 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

For starters, I installed PYTHONHOME in 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 is still pointing to python 24 and nothing is changing. The setx command does not change the environment variable. What causes this error?

+7
source share
4 answers

Windows command line error:

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

Summary:

You use the setx command and assign it several tokens when only one is allowed.

How to reproduce this error in Windows:

Windows cmd . :

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.

, :

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

​​, cmd .

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

. .

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

cmd , . .

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

FANCYPANTS .

+6

SETX , , \".

.

CMD, script. ENDLOCAL script script. script , ENDLOCAL.

@echo on
setlocal enableDelayedExpansion
if "!PYTHONHOME:~-2!" == "24" (
  set "PYTHONHOME=C:\Python33"
  set "PATH=!PATH:Python24=Python33!"
) else (
  set "PYTHONHOME=C:\Python24"
  set "PATH=!PATH:Python33=Python24!"
)
setx PYTHONHOME "!home!"
setx PATH "!path:"=\"!"
pause
+3

SETX . , :

SETX PATH "%PATH%;Path to new thing added" /M

, . , .

0

:

@setlocal enableextensions enabledelayedexpansion
@echo off
set str1=%PYCURRENTPATHS%

if not "x%str1:python2=%" == "x%str1%"  (
    set PYCURRENTPATHS=%PY3PATHS%
) else (
    set PYCURRENTPATHS=%PY2PATHS%
) 
setx PYCURRENTPATHS %PYCURRENTPATHS%
set PATH=%PATH%
endlocal

3 : ( "set" , "setx" )

set PY2PATHS=D:\ProgramData\Anaconda3\env\python2;D:\ProgramData\Anaconda3\env\python2\Scripts
set PY3PATHS=D:\ProgramData\Anaconda3;D:\ProgramData\Anaconda3\Scripts
setx PY2PATHS %PY2PATHS%
setx PY3PATHS %PY3PATHS%
setx PYCURRENTPATHS %PY2PATHS%

"% PYCURRENTPATHS%" : Paths

anaconda python2 : conda create -n python2 python = 2.7 anaconda

Windows Python . , , , .

1) Anaconda ( python 3 )... / python, - Anaconda ( pip url :()

2) python2 anaconda: conda create -n python2 python = 2.7 anaconda

3) , python (, )

Python, (, PY2Tensor, Py3Scikit ..) conda :)

Anaconda . , , .

Linux , Windows Python, .

: set PATH =% PATH% "PATH" + "% PYCURRENTPATHS%" ,

0

All Articles