视窗检查字符串包含另一个不工作 [英] Windows check string contains another not working
问题描述
我有这个批处理文件添加一个环境变量,如果不存在
I have this batch file to append a environment variable if not exists
setLocal EnableDelayedExpansion
set echo off
set envPath=%PATH%
set comPath=";D:\Package\Libraries\Lib"
if x%envPath:comPath=%==x%envPath% (
setx PATH "%PATH%;D:\Package\Libraries\Lib" /M
)
pause
但它没有工作,说文件是意外此时
But its not working and says file was unexpected this time
我写依据一href=\"http://stackoverflow.com/questions/7005951/batch-file-find-if-substring-is-in-string-not-in-a-file\">Batch文件:如果找到的子字符串(而不是在一个文件中)
I wrote based on Batch file: Find if substring is in string (not in a file)
推荐答案
如上面的评论中提及,利用推迟的主串的扩张,而替换字符串中的常规扩张的。从快捷方式或从管理的命令提示符运行这个批处理以Admin:
As mentioned in the above comment, use delayed expansion in the main string, and regular expansion in the replace string. Run this batch as Admin from shortcut or from Admin Cmd Prompt:
@echo off
setLocal EnableDelayedExpansion
set "comPath=D:\Package\Libraries\Lib"
set "envPath=%PATH%" & set "Separator="
if not "%envPath:~-1%" == ";" set "Separator=;"
if "!envPath:%comPath%=!"=="%envPath%" (
setx PATH "%PATH%%Separator%%comPath%" /M )
timeout 5
exit /b
请注意,更新的路径将从注册只有在Cmd的重启重新读取。如果你需要使用修改后的路径在同一批次,使用SET代替SETX临时设置PATH为Cmd的会话。
Note that updated PATH will be re-read from Registry only upon Cmd restart. If you need to use the amended PATH in the same batch, use SET instead of SETX to set the PATH temporarily for that Cmd session.
在一个类似的结构,如果额外的路径 comPath
设置的在的IF或FOR循环,使用调用集PATH = %% envPath:!comPath = %%。
而不是
In a similar construct, if your extra path comPath
is set inside IF or FOR loop, use call set "PATH=%%envPath:!comPath!=%%"
instead.
这篇关于视窗检查字符串包含另一个不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!