使用 REG QUERY、REG EXPORT、列表匹配、REG DELETE(如果匹配)以及 ECHO 任何输出到 CMD 和 output.log 进行批处理 FOR LOOP

使用 REG QUERY、REG EXPORT、列表匹配、REG DELETE(如果匹配)以及 ECHO 任何输出到 CMD 和 output.log 进行批处理 FOR LOOP

我有一个要删除的注册表项/值/条目列表(基本上是下面 #3 中的列表,没有 REG DELETE 内容)。它按原样工作,但我想进行批处理...

I. 如果 %~dp0 中不存在 list.txt,则写入 listfile.txt 本身

例子

set "ListFile=%TEMP%\ListFile.txt"
set "List=%~dp0list.txt"

if not exist "%List%" (
    (
        echo Some regisrtry key
        echo Some registry value
        echo Some registry subkey
        echo No idea how it should all be written like
    ) >"%ListFile%"
)

II. 使用 REG QUERY 检查列表中每个条目是否存在,并查看 ERRORLEVEL

如果存在的话..

  1. REG EXPORT 将其导出为其自己的有效 .reg(如果出现问题可以将其重新导入)。

例子

for /f %%A in (%src%) do (
    if not exist "%bkp%\%%A" md "%bkp%\%%A"
    reg export "%%A" "%bkp%\%%A\key.reg"

我不确定上述操作是否适用于特定值或指定键内的所有条目。

  1. 比较列表和 .reg

  2. 如果匹配,则根据情况运行 REG DELETE /f 或 REG DELETE /v /f 或 REG DELETE /va /f。

REG DELETE "HKCU\Software\Google\Chrome" /f
REG DELETE "HKLM\Software\Google\Chrome" /f
REG DELETE "HKCU\Software\Policies\Google\Chrome" /f
REG DELETE "HKLM\Software\Policies\Google\Chrome" /f
REG DELETE "HKLM\Software\Policies\Google\Update" /f
REG DELETE "HKLM\Software\WOW6432Node\Google\Enrollment" /f
REG DELETE "HKLM\Software\WOW6432Node\Google\Update\ClientState\{430FD4D0-B729-4F61-AA34-91526481799D}" /v CloudManagementEnrollmentToken /f
REG DELETE "HKLM\Software\WOW6432Node\Microsoft\Policies" /f
REG DELETE "HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Policies" /f
REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies" /f
REG DELETE "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies" /f
REG DELETE "HKCU\Software\Microsoft\WindowsSelfHost" /f
REG DELETE "HKLM\Software\Microsoft\WindowsSelfHost" /f
REG DELETE "HKLM\Software\Microsoft\Policies" /f
REG DELETE "HKCU\Software\Policies" /f
REG DELETE "HKLM\Software\Policies" /f
REG DELETE "HKLM\Software\Policies\Microsoft\Windows Defender" /v DisableAntiSpyware /f
REG DELETE "HKLM\Software\Policies\Microsoft\Edge" /va /f
REG DELETE "HKLM\Software\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate" /f
REG DELETE "HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate" /f
  1. ECHO 到 cmd 和 %~dp0output.log,键/值/条目已被处理并删除

如果不存在的话..

  1. ECHO 到 cmd 和 %~dp0output.log 它不存在
"HKCU\Software\Google\Chrome" key does not exist
No entries can be found in "HKLM\Software\Policies\Microsoft\Edge"
Value DisableAntiSpyware cannot be found in "HKLM\Software\Policies\Microsoft\Windows Defender"

III. 删除批次创建的列表,但如果该列表已经存在则不删除。

IF EXIST "%ListFile%" ( DEL "%ListFile%" ) ELSE ( goto :eof )

我试图修改我找到的代码片段,但仍然不明白 for 循环是如何工作的,也不明白我只有一个列表,并且 for 循环会根据它是一个键、一个值(/v)和/或只需要处理指定键内的所有条目(/va)的情况进行调整

下面的当前脚本到目前为止可以工作,但缺少上述所有要点。任何帮助添加这些内容的帮助都将不胜感激。

REG DELETE "HKCU\Software\Google\Chrome" /f
REG DELETE "HKLM\Software\Google\Chrome" /f
REG DELETE "HKCU\Software\Policies\Google\Chrome" /f
REG DELETE "HKLM\Software\Policies\Google\Chrome" /f
REG DELETE "HKLM\Software\Policies\Google\Update" /f
REG DELETE "HKLM\Software\WOW6432Node\Google\Enrollment" /f
REG DELETE "HKLM\Software\WOW6432Node\Google\Update\ClientState\{430FD4D0-B729-4F61-AA34-91526481799D}" /v CloudManagementEnrollmentToken /f
REG DELETE "HKLM\Software\WOW6432Node\Microsoft\Policies" /f
REG DELETE "HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Policies" /f
REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies" /f
REG DELETE "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies" /f
REG DELETE "HKCU\Software\Microsoft\WindowsSelfHost" /f
REG DELETE "HKLM\Software\Microsoft\WindowsSelfHost" /f
REG DELETE "HKLM\Software\Microsoft\Policies" /f
REG DELETE "HKCU\Software\Policies" /f
REG DELETE "HKLM\Software\Policies" /f
REG DELETE "HKLM\Software\Policies\Microsoft\Windows Defender" /v DisableAntiSpyware /f
REG DELETE "HKLM\Software\Policies\Microsoft\Edge" /va /f
REG DELETE "HKLM\Software\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate" /f
REG DELETE "HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate" /f

@RD /S /Q "%ProgramFiles(x86)%\Google\Policies"
@RD /S /Q "%WinDir%\System32\GroupPolicy"
@RD /S /Q "%WinDir%\System32\GroupPolicyUsers"

ECHO.
gpupdate /force
ECHO.
ECHO Complete. Will shutdown..
pause>nul
shutdown.exe /r /t 00
EXIT /B 0

答案1

@echo off

for /f usebackq^tokens^=1-3*delims^=^" %%i in =;(
    `type "%~f0" ^| findstr /b \^"`
        );= do if "%%~l" == "" 2>nul =;(
             reg query "%%~i" >nul && =;(
             reg export "%%~i" "%%~k.reg" /y
             reg delete "%%~i" /f
            );= || echo/Key not exist "%%~i"
        );= else if "%%~l" == "/va" 2>nul =;(
             reg query "%%~i" >nul && =;(
             reg export "%%~i" "%%~k.reg" /y
             reg delete "%%~i" /va /f
            );= || echo/Key not exist "%%~i"
        );= else 2>nul =;(
             reg query "%%~i" >nul && =;(
             reg export "%%~i" "%%~l.reg" /y
             reg delete "%%~i" /v "%%~j" /f
            );= || echo/Key not exist "%%~i"
        );=

>nul 2>&1 rd /s /q "%ProgramFiles(x86)%\Google\Policies" "%Windir%\System32\GroupPolicy" "%Windir%\System32\GroupPolicyUsers"

echo/ & gpupdate /force | findstr . 
timeout -1 | echo/Complete. Will shutdown!.. 

echo/ & shutdown.exe /r /t 00   
exit /b 0 || goto :eof

::--------------------------------------------------------------::
::         Your listing of registry keys/values/entries         ::
::--------------------------------------------------------------::

"HKCU\Software\Policies" "HKCU_Policies"
"HKCU\Software\Google\Chrome" "HKCU_Chrome"
"HKCU\Software\Policies\Google\Chrome" "HKCU_Google_Chrome"
"HKCU\Software\Microsoft\WindowsSelfHost" "HKCU_Microsoft_WindowsSelfHost"
"HKCU\Software\Microsoft\Windows\CurrentVersion\Policies" "HKCU_Microsoft_Policies"

"HKLM\Software\Policies" "HKLM_Policies"
"HKLM\Software\Google\Chrome" "HKLM_Chrome"
"HKLM\Software\Microsoft\Policies" "HKLM_Microsoft_Policies"
"HKLM\Software\Policies\Google\Chrome" "HKLM_Google_Chrome"
"HKLM\Software\Policies\Google\Update" "HKLM_Google_Update"
"HKLM\Software\Microsoft\WindowsSelfHost" "HKLM_Microsoft_WindowsSelfHost"
"HKLM\Software\Policies\Microsoft\Edge" "HKLM_Edge" /va

"HKLM\Software\Microsoft\Windows\CurrentVersion\Policies" "HKLM_CurrentVersion_Policies"
"HKLM\Software\Policies\Microsoft\Windows Defender" "HKLM_Windows_Defender" "DisableAntiSpyware"
"HKLM\Software\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate" "HKLM_WindowsStore_WindowsUpdate"

"HKLM\Software\WOW6432Node\Google\Enrollment" "HKLM_WOW6432Node_Google_Enrollment"
"HKLM\Software\WOW6432Node\Microsoft\Policies" "HKLM_WOW6432Node_Microsoft_Policies"
"HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Policies" "HKLM_WOW6432Node_CurrentVersion_Policies"
"HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate" "HKLM_WOW6432Node_WindowsStore_WindowsUpdate"
"HKLM\Software\WOW6432Node\Google\Update\ClientState\{430FD4D0-B729-4F61-AA34-91526481799D}" "HKLM_WOW6432Node_Google_Update_ClientState" "CloudManagementEnrollmentToken"

1.使用批处理存储列出 files.reg 的密钥和名称(唯一值)

2.使用For /F循环查找/过滤键

3.For /F选择在此循环中使用“引号”作为分隔符

4.在循环命令中,过滤掉以"(引述)如下:

  • for ...('
             type "%~f0" ^|findstr /b \^" 
                                          ')do...
    

5.使用出现的值(空、相等或不同)来%%l处理if每种情况

6.使用&&运算符来判断reg query成功与否,可以告诉你注册表中是否存在键/值

6.使用&&运算符来判断reg query成功与否,可以告诉你注册表中是否存在键/值


观察1:您可以使用一个命令删除多个文件夹:

   rd /q /s "FOLDER_01" "FOLDER_02" "FOLDER_nn"
rmdir /q /s "FOLDER_01" "FOLDER_02" "FOLDER_nn"

观察2:您可以删除不存在的文件夹,它不会返回核宇宙错误,它只会返回警告“系统找不到”因此,您可以在删除文件夹时省略可能的简单文本消息(无论文件夹是否存在),它甚至不会中止您的 bat 的执行:

2>nul rmdir /q /s "FOLDER_01" "FOLDER_02" "FOLDER_nn"
if exist "FOLDER_01" rd /q /s "FOLDER_01"
if exist "FOLDER_02" rd /q /s "FOLDER_02"
if exist "FOLDER_nn" rd /q /s "FOLDER_nn"

观察3:删除命令同样如此:

2>nul del /q "FILE_01" "FILE_02" "FILE_nn"
if exist "FILE_01" del /q "FILE_01"
if exist "FILE_02" del /q "FILE_02"
if exist "FILE_nn" del /q "FILE_nn"

其他资源:

相关内容