批处理:更新系统变量的字符串"路径"

时间:2018-01-29 17:41:19

标签: batch-file variables path system

我正在尝试查询系统PATH变量以准备更新。

  • 如果旧字符串存在,请删除并追加新字符串(x2) 路径的开头。
  • 如果新字符串已经在路径中,则不执行任何操作
  • 如果新路径或旧路径都不在路径中,则将新路径添加到路径的开头。

我已经编写了一个批处理,但是如果我没有先清除%errorlevel时已经附加了新路径,或者如果我清除了%errorlevel,那么它将会中断,无论它是否已经存在,它都会简单地添加新路径有没有。我相信有一种更简单的方法可以做到这一点。

@echo off
setlocal enableextensions disabledelayedexpansion
type nul > C:\_sandbox\%computername%_PATH_UPDATE.log
echo %DATE% - %TIME% - PATH UPDATE started >> C:\_sandbox\%computername%_PATH_UPDATE.log
echo. >> C:\_sandbox\%computername%_PATH_UPDATE.log
echo Checking for OLD_PATH >> C:\_sandbox\%computername%_PATH_UPDATE.log
set "ErrorLevel="
echo %PATH% | FINDSTR /IX  "C:\Program Files (x86)\OLD_PATH"
If %ErrorLevel%==0 goto 0
If %ErrorLevel%==1 goto 1

:0
set "ErrorLevel="
echo OLD_PATH found, removing and appending new path  >> C:\_sandbox\%computername%_PATH_UPDATE.log
set NEWPATH=C:\Program Files (x86)\NEW_PATH;C:\Program Files (x86)\NEW_PATH2;%PATH:;C:\Program Files (x86)\OLD_PATH=%
setx PATH "%NEWPATH%" /m
exit /b 0

:1
set "ErrorLevel="
echo. >> C:\_sandbox\%computername%_PATH_UPDATE.log
echo OLD_PATH not found, checking for NEW_PATH  >> C:\_sandbox\%computername%_PATH_UPDATE.log
echo %PATH% | FINDSTR /IX  "C:\Program Files (x86)\NEW_PATH2"
If %ErrorLevel%==0 goto 2
If %ErrorLevel%==1 goto 3

:2
set "ErrorLevel="
echo NEW_PATH found, no further action required  >> C:\_sandbox\%computername%_PATH_UPDATE.log
echo %path%
exit /b 0

:3
set "ErrorLevel="
echo NEW_PATH nor OLD_PATH were found, appending NEW_PATH  >> C:\_sandbox\%computername%_PATH_UPDATE.log
set NEWPATH=C:\Program Files (x86)\NEW_PATH;C:\Program Files (x86)\NEW_PATH2;%PATH:;C:\Program Files (x86)\OLD_PATH=%
setx PATH "%NEWPATH%" /m
echo NEW_PATH Should of been appended.  >> C:\_sandbox\%computername%_PATH_UPDATE.log
exit /b 0

我不需要记录,但是为了进行测试,我一直在记录所有内容,而不是使用暂停,因为日志记录可能对部署有用。 Batch将作为SYSTEM运行,我想这将需要用户重新登录。

在我们的实例中,OLD PATH(例如)是C:\ Program Files(x86)\ Microsoft \ Notepadv1.2 - 其中NEW_PATH是C:\ Program Files(x86)\ Microsoft \ Notepad和C:\ Program文件(x86)\ Microsoft \ Notepad_x64分别。这就是我在FINDSTR中使用/ IX标志查询NEW_PATH2的原因。

2 个答案:

答案 0 :(得分:0)

试试这个:

masterList.union(insertList.except(masterList))

+------+--------+ |ttm_id|audit_id| +------+--------+ |1 |10 | |15 |10 | |15 |9 | +------+--------+ 就是您要搜索的内容。

此外,您永远不应该尝试写入@ECHO OFF SETLOCAL EnableExtensions DisableDelayedExpansion TYPE nul > C:\_sandbox\%computername%_PATH_UPDATE.log ECHO %DATE% - %TIME% - PATH UPDATE started >> C:\_sandbox\%computername%_PATH_UPDATE.log ECHO. >> C:\_sandbox\%computername%_PATH_UPDATE.log ECHO Checking for OLD_PATH >> C:\_sandbox\%computername%_PATH_UPDATE.log ECHO %PATH% | FINDSTR /I /C:"C:\Program Files (x86)\OLD_PATH" IF %ERRORLEVEL%==0 GOTO 0 IF %ERRORLEVEL%==1 GOTO 1 :0 ECHO OLD_PATH found and removed, NEW_PATH appended >> C:\_sandbox\%computername%_PATH_UPDATE.log SET NEWPATH=C:\Program Files (x86)\NEW_PATH;C:\Program Files (x86)\NEW_PATH2;%PATH:C:\Program Files (x86)\OLD_PATH;=% SETX PATH "%NEWPATH%" /m EXIT /B :1 ECHO. >> C:\_sandbox\%computername%_PATH_UPDATE.log ECHO OLD_PATH not found, checking for NEW_PATH >> C:\_sandbox\%computername%_PATH_UPDATE.log ECHO %PATH% | FINDSTR /I /C:"C:\Program Files (x86)\NEW_PATH2" IF %ERRORLEVEL%==0 GOTO 2 IF %ERRORLEVEL%==1 GOTO 3 :2 ECHO NEW_PATH found, no further action required >> C:\_sandbox\%computername%_PATH_UPDATE.log ECHO %PATH% EXIT /B :3 ECHO NEW_PATH nor OLD_PATH were found, NEW_PATH appended >> C:\_sandbox\%computername%_PATH_UPDATE.log SET NEWPATH=C:\Program Files (x86)\NEW_PATH;C:\Program Files (x86)\NEW_PATH2;%PATH:C:\Program Files (x86)\OLD_PATH;=% SETX PATH "%NEWPATH%" /m EXIT /B 变量,因为您设置的值将创建一个名为FINDSTR /C:的用户变量,该变量优先于内部伪变量%ERRORLEVEL%read more)。

如果您需要特定的ERRORLEVEL,可以使用:

  • ERRORLEVEL代表ERRORLEVEL CALL SET
  • ERRORLEVEL代表0 COLOR 00
  • ERRORLEVEL代表1 EXIT /B n

具有讽刺意味的是,您重置ERRORLEVEL的尝试不会创建用户变量,也不会更改n

答案 1 :(得分:0)

搞定了。更新了任何人未来的搜索。

  • path.old =旧路径
  • path = new path

我们的旧路径中有一个内部产品的版本号。新路径不再具有版本号。它们很相似,造成了所有这些麻烦。

@echo off
setlocal enableextensions disabledelayedexpansion
ver > nul
echo %path% | FINDSTR /I ".*old.*"
If %ErrorLevel%==0 goto 0
If %ErrorLevel%==1 goto 1

:0
ver > nul
set NEWPATH=C:\Program Files (x86)\path_x64;C:\Program Files (x86)\path;%PATH:;C:\Program Files (x86)\path.old=%
setx /m PATH "%NEWPATH%"
exit

:1
ver > nul
echo %path% | FINDSTR /I ".*path_x64"
If %ErrorLevel%==0 exit
If %ErrorLevel%==1 goto 2

:2
set NEWPATH=C:\Program Files (x86)\newpath_x64;C:\Program Files (x86)\newpath;%PATH%
setx /m PATH "%NEWPATH%"
exit