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

查看:15
本文介绍了如何从批处理文件中持久设置 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.

我正在使用这个 windows 命令:

I am using this windows command:

set PATH=%cd%;%path%
pause

但是它似乎只对这个 cmd 实例有效.我希望它是永久性的,因为我首先设置了 PATH,然后运行了一个需要在该文件夹中找到库的程序.

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代替set.

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天全站免登陆