如何坚持从一个批处理文件设置一个变量在Windows 7? [英] How to persistently set a variable in Windows 7 from a batch file?

查看:125
本文介绍了如何坚持从一个批处理文件设置一个变量在Windows 7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置使用BAT文件的Windows 7 PATH环境变量;然而,它似乎并没有工作。

I am trying to set the PATH environment variable in windows 7 using a bat-file; however it does not seem to work.

我使用该窗口的命令:

set PATH=%cd%;%path%
pause

然而,它似乎只此CMD实例是有效的。我希望它是永久性的,因为我第一次设定的路径,然后运行一个程序,它需要找到该文件夹​​中的库。

However it only appears to be valid for this cmd instance. I want it to be permanent, since I first set the PATH and then run a program which needs to locate the libraries in that folder.

推荐答案

使用setx.exe,而不是集。

Use setx.exe instead of set.

setx PATH "%cd%;%path%;"
pause

请注意,这将路径设置为所有未来CMD的情况下,但是的的当前之一。如果你需要,也跑你原来的set命令。

Note that this sets the path for all future cmd instances, but not for the current one. If you need that, also run your original set command.

更新:第二个参数需要被引用如果它包含空格(%PATH%历来如此)。被警告说,如果在你的%PATH%的最后一个字符是反斜杠,它会逃跑尾随报价和最后的路径条目将停止工作。我避开了被封闭引号之前追加一个分号。

UPDATE: The second parameter needs to be quoted if it contains spaces (which %path% always has). Be warned that if the last character in your %path% is a backslash, it will escape the trailing quote and the last path entry will stop working. I get around that by appending a semicolon before the closing quote.

如果你不想冒险让;;;;;;在你的路径的重复运行结束后,再剥离,而不是从%PATH%变量的任何尾部的反斜杠设置之前,它会正常工作。

If you don't want to risk getting ";;;;;;" at the end of your path after repeated runs, then instead strip any trailing backslash from the %path% variable before setting, and it will work correctly.

这篇关于如何坚持从一个批处理文件设置一个变量在Windows 7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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