How to use setx command in a windows batch file

前端 未结 4 950
轻奢々
轻奢々 2021-02-09 10:05

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 sys

4条回答
  •  爱一瞬间的悲伤
    2021-02-09 10:26

    SETX requires values with spaces to be quoted, and quotes within the value must be escaped as \".

    Best also to use delayed expansion to protect against special characters during the batch parsing phase.

    The following will not only toggle the values for new CMD sessions, it will also toggle the value for the remainder of the batch script run. An implicit ENDLOCAL at the end of the script will revert to the old values within the current session once the script ends. If needed, the script can be modified to preserve the new values past the ENDLOCAL barrier.

    @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
    

提交回复
热议问题