我用这段代码创建了 regkey_path_bak.txt
SET Key="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
ECHO %CurrPath%> regkey_path_bak.txt
C:\Program Files (x86)\VMware\VMware Workstation\bin\;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Usuario\Manolo\Bin;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\AMD\ATI.ACE\Core-Static;C:\Program Files\dotnet;C:\Usuario1\Manolo\Bin;C:\Soft\Bin
现在我希望用分号分隔
然后...删除所有名为 MANOLO 的行
并将其全部合并到 regkey_newpath_bak.txt 中(它应该保持这样)
C:\Program Files (x86)\VMware\VMware Workstation\bin\;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\AMD\ATI.ACE\Core-Static;C:\Program Files\dotnet;C:\Soft\Bin
答案1
@HugoCar 这是一个批处理文件,它可以执行您想要的操作。
您可以在任何路径字符串上调用 :PARSE_PATH。我在某些地方添加了注释,以便您了解发生了什么
@echo off
SET REG_KEY="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
SET BAK_FILE=reg_key_bak.txt
SET SKIP_KW=manolo
SET NEW_FILE=reg_key_new.txt
FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %REG_KEY% /v PATH`) DO (
call :PARSE_PATH "%%B"
)
REM you can call :PARSE_PATH with an existing path, or a string with a path
REM call :PARSE_PATH "%PATH%"
REM this removes the keyword
findstr /i /v /c:"%SKIP_KW%" %BAK_FILE% > %NEW_FILE%
SET APATH=
FOR /F "usebackq tokens=*" %%I IN (%NEW_FILE%) DO (
call :ADD_PATH "%%I"
)
echo %APATH%
REM remove first character, extra semi-colon
SET PATH=%APATH:~1%
REM unset everything
SET REG_KEY=
SET BAK_FILE=
SET SKIP_KW=
SET NEW_FILE=
exit /b 0
REM credit goes to https://stackoverflow.com/a/19009701 for this routine
:PARSE_PATH
setlocal
SET T=%~1
del %BAK_FILE% 2>nul
:LOOP
FOR /F "tokens=1* delims=;" %%I IN ("%T%") DO (
echo %%I>> %BAK_FILE%
SET T=%%J
)
IF DEFINED T GOTO :LOOP
exit /b 0
REM this routine concats the path
:ADD_PATH
SET APATH=%APATH%;%~1
exit /b 0