
在 CMD 提示字元下一切正常,但是,我需要放入一個 bat 檔案來運行,但無法取得 Reg Query cmd 的輸出,然後從中執行另一個查詢。
由於我正在尋找的是應用程式 ID,並且它可以更改,因此我需要找到密鑰的其餘部分,命令提示字元中的以下內容可以滿足我的需要:
REG Query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall /F "APP_NAME" /E /S
給我所需的金鑰,例如:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\{398E49A0-84CA-43B5-A926-42EF68619E91}
從那裡我可以運行另一個命令來獲取卸載命令:
REG Query HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\{398E49A0-84CA-43B5-A926-42EF68619E91} /V "UninstallString"
我已經嘗試過在網站上使用的內容,for \f token....
但即使使用跳過,它也不會只給我第一個字串。我也無法以某種方式分配它。我需要類似的東西:
set Full_Key = reg query...
set Uninstall_CMD = Full_Key "/V Uninstallstring"
我知道一旦找到,就會只有 1 個條目。
答案1
經過大量的作業,並翻閱了我的舊書,我終於成功了。在這裡發布程式碼,以防有人需要在網路上卸載與我需要的相同的應用程序,或者需要一個起點。
我必須解決的主要問題是找到應用程式 ID,然後從註冊表中取得卸載字串,因為沒有典型的卸載檔案。
可能不是最好看的程式碼,但確實有效。
rem First Check To See If The App Is Even Installed, In My Case, It May Not Be A Dell Box, Or We Already Uninstalled
REG Query %Computer%HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /F "Dell SupportAssist" /E /D /S > NUL
Rem If We have Error, It Is Not There, Bail Out
if errorlevel 1 exit
rem The Key Is Found, So It Must Be Installed, So Get The Full Key, By Searching For The App Name
FOR /F "tokens=*" %%A IN ('REG Query %Computer%HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /F "Dell SupportAssist" /E /D /S 2^>NUL ^| FINDSTR /R /C:"HKEY_"') DO (
rem Now We Have The Full Key From Above, Grab The Uninstall Command And Run It
FOR /F "tokens=2*" %%B IN ('REG Query "%Computer%%%~A" /F UninstallString /V /E ^| FIND /I " UninstallString "') DO %%C% /qn
)