批量修改PATH变量 [英] Modifying the PATH variable from batch

查看:57
本文介绍了批量修改PATH变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试搜索Windows路径变量,以查看其中是否存在目录.如果没有,则添加它.

I am attempting to search the windows path variable to see if a directory exists in it. And If it does not, add it.

我的代码如下:

@echo off

SET VAR1=%path%

echo %VAR1% > text.txt

FOR /f "tokens=* delims=;" %%a IN (text.txt) DO (
echo.%%a|findstr /C:"app0" >nul 2>&1
    if not errorlevel 1 (
    echo Directory was Found
    ) else (
    SET PATH=%PATH%;%cd%\app0
    )
)

我遇到的问题必须出现在else语句中.当我用echo NOT FOUND替换SET PATH时,一切工作正常.但是,当我使用该行来设置path变量时,它返回"\ Common这一次是意外的".而%cd%不应包含"common",因为我是从台式机运行的

The issue I am getting must occur in the else statement. When I replace the SET PATH with echo NOT FOUND everything works perfectly. However, when I use the line to set the path variable, it returns "\Common was unexpected at this time"; while %cd% should not include "common" as I am running from the desktop

我以前运行过一次,它运行完美,一切正常,我重新启动,现在相同的代码无法达到相同的结果.

I had run it once before and it ran perfectly and everything functioned, I went to reboot and now the same code does not achieve the same result.

经过调试后,我发现问题实际上出在echo %VAR1% > text.txt

After some debugging, I found that the issue is actually in the line echo %VAR1% > text.txt

打开回显将显示以下内容:

Turning echo on displays the following:

echo C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R)
> Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R)
> Management Engine
> Components\iCLS\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program
> Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program
> Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files
> (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program
> Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program
> Files\Intel\WiFi\bin\;C:\Program Files\Common
> Files\Intel\WirelessCommon\;C:\Program Files (x86)\Common
> Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R)
> Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R)
> Management Engine
> Components\iCLS\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program
> Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program
> Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files
> (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program
> Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program
> Files\Intel\WiFi\bin\;C:\Program Files\Common
> Files\Intel\WirelessCommon\;C:\Users\Bryan
> Douglas\AppData\Local\Microsoft\WindowsApps;C:\Program
> Files\Intel\WiFi\bin\;C:\Program Files\Common
> Files\Intel\WirelessCommon\;  1>text.txt
>     \Common was unexpected at this time.

我做错什么了吗?

推荐答案

仅解决您的实际问题:

您的PATH包含右括号,例如:

Your PATH contains closing parentheses, for example:

C:\Program Files (x86)\Common Files\Oracle\Java\javapath
                     ^ here

此括号在set命令处关闭命令块:

This parenthesis closes your command block at your set command:

SET PATH=%PATH%;%cd%\app0
           ^ inside here

)之后的下一个内容是\Common Files\Oracle\Java\javapath,它被解释为带有参数Files\Oracle\Java\javapath的命令\Common.因此,您得到:

The next thing after the ) is \Common Files\Oracle\Java\javapath, which is interpreted as a command \Common with a parameter of Files\Oracle\Java\javapath. So consequently you get:

\ Common在此时是意外的.

\Common was unexpected at this time.

使用首选语法set "var=value"(注意引号的位置).引号可以保护)&| ...

Use the preferred syntax set "var=value" (note the position of the quotes). The quotes secure poisonous chars like )&|...

因此,您的代码仅使用首选的set语法即可工作:

So your code works with just using the preferred set syntax:

SET "PATH=%PATH%;%cd%\app0"

这篇关于批量修改PATH变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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