批次 FOR LOOP 與 REG QUERY、REG EXPORT、列表匹配、REG DELETE(如果匹配)以及 ECHO 任何輸出到 CMD 和 output.log

批次 FOR LOOP 與 REG QUERY、REG EXPORT、列表匹配、REG DELETE(如果匹配)以及 ECHO 任何輸出到 CMD 和 output.log

我有一個要刪除的註冊表項/值/條目的列表(基本上是下面#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%"
)

二.使用 REG QUERY 檢查清單中每個項是否存在,並有一個 ERRORLEVEL

如果存在..

  1. REG 將其匯出到自己的有效 .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"

三.刪除該批次建立的列表,但如果該列表已存在,則不刪除。

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.使用 an 中出現的值(空、相等或不同)來%%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"

其他資源:

相關內容