如何在 Windows 批处理文件中使用 setx 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14442734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:12:32  来源:igfitidea点击:

How to use setx command in a windows batch file

windowsbatch-fileenvironment-variablessetx

提问by code82

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

我正在尝试创建一个 Windows 批处理文件来自动设置环境变量以使用 python 2.4 或 python 3.3。

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

我的系统上安装了 python 2.4 和 3.3。这是我的代码:

::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

首先,我将 PYTHONHOME 设置为 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?

我的 PYTHONHOME 仍然指向 python 24,没有任何改变。setx 命令不会更改环境变量。是什么导致了这个错误?

采纳答案by Eric Leschinski

The Windows command line error:

Windows 命令行错误:

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.

您正在使用 setx 命令并在只允许一个令牌时为其分配多个令牌。

How to reproduce this error on Windows:

如何在 Windows 上重现此错误:

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

打开 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.

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.

变量已设置,在此处重新启动 cmd 终端以加载更改。

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.

在此处重新启动 cmd 终端以加载更改。再次打印内容。

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

the variable FANCYPANTS was deleted and no longer exists.

变量 FANCYPANTS 被删除,不再存在。

回答by dbenham

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

SETX 需要引用带空格的值,并且值中的引号必须转义为\".

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 sessiononce the script ends. If needed, the script can be modified to preserve the new values past the ENDLOCAL barrier.

以下不仅会切换新 CMD 会话的值,还会切换批处理脚本运行的其余部分的值。一旦脚本结束,脚本末尾的隐式 ENDLOCAL 将恢复为当前会话中的旧值。如果需要,可以修改脚本以保留越过 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

回答by diegeelvis_SA

The SETXcommand is very reliant on the syntax of the command. The following example shows the basic syntax to use to set the path environment variable:

SETX命令非常依赖于命令的语法。以下示例显示了用于设置路径环境变量的基本语法:

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

This will also add the new path to the system registry, but still won't add it for the current session. Re-launch the terminal for it to take affect.

这也会将新路径添加到系统注册表,但仍不会为当前会话添加它。重新启动终端使其生效。

回答by James Nelson

I really like this way

我真的很喜欢这种方式

here's the batch script:

这是批处理脚本:

@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

we'll need 3 variables : (use "set" to set current terminal, use "setx" to set persistent variable)

我们需要 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%

And add "%PYCURRENTPATHS%" to your path via GUI: Paths

并通过 GUI 将“%PYCURRENTPATHS%”添加到您的路径中: 路径

This example uses anaconda and python2 setup from this example: conda create -n python2 python=2.7 anaconda

此示例使用此示例中的 anaconda 和 python2 设置: conda create -n python2 python=2.7 anaconda

So for ultra painless Windows python, I can't recommend the following solution enough. Please give it a try I think you'll like it.

所以对于超无痛 Windows python,我不能推荐以下解决方案。请试一试,我想你会喜欢的。

1) use Anaconda (start with python 3 for this example)... for the longest time I resisted and had better luck with manual installs/management of python, but due to network issues, I was forced to use Anaconda (didn't want to allow pip url on network :( )

1) 使用 Anaconda(本例从 python 3 开始)...在我最长时间内,我抵制并在手动安装/管理 python 方面有更好的运气,但由于网络问题,我被迫使用 Anaconda(没有想要在网络上允许 pip url :()

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

2)从anaconda提示符安装python2:conda create -n python2 python=2.7 anaconda

3) create the script above to make your python installs available to command line (e.g. add to path)

3)创建上面的脚本以使您的python安装可用于命令行(例如添加到路径)

You can add more virtual python environments, set them in the command line or enhance this script as well (eg PY2Tensor, Py3Scikit, etc.) or simply manage them via conda :)

你可以添加更多的虚拟 python 环境,在命令行中设置它们或增强这个脚本(例如 PY2Tensor、Py3Scikit 等)或简单地通过 conda 管理它们:)

Anaconda benefits from pre-compiled packages. No package mismatches, unstable releases, legacy issues or broken dependencies.

Anaconda 受益于预编译的软件包。没有包不匹配、不稳定的版本、遗留问题或破坏的依赖关系。

I still prefer Linux for development, but if you have to use windows and python, it's gotten better.

我仍然更喜欢Linux进行开发,但是如果您必须使用Windows和python,那就更好了。

Note: set PATH=%PATH% causes re-eval of "PATH" + "%PYCURRENTPATHS%" in current session, a new session will reflect the change without this line

注意:set PATH=%PATH% 导致在当前会话中重新评估“PATH”+“%PYCURRENTPATHS%”,一个新的会话将反映没有这一行的变化