如何在Windows批处理文件中使用命令SETX [英] How to use setx command in a windows batch file

查看:1707
本文介绍了如何在Windows批处理文件中使用命令SETX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Windows批处理文件来自动设置环境变量来使用Python 2.4或Python 3.3。

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

无论是Python 2.4和3.3都安装我的系统上。这里是我的code:

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

要开始我已经PYTHONHOME设置为 C:\\ Python24

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

但是,上面的脚本提供了以下错误:

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

我PYTHONHOME仍然指向到Python 24并没有什么改变。该SETX命令不更改环境变量。是什么原因造成这个错误?

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?

推荐答案

Windows命令行错误:

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

摘要:

您使用的是SETX命令,当只有一个被允许为其指定多个标记。

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

如何重现在Windows这个错误:

CMD打开一个终端窗口并输入以下命令。这引发错误:

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>

的变量设置,在这里重新启动CMD终端负载的变化。

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.

在这里重新启动CMD终端加载的变化。打印内容一次。

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

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

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

the variable FANCYPANTS was deleted and no longer exists.

这篇关于如何在Windows批处理文件中使用命令SETX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆