删除所有包含或命名为“字符串"的注册表项.与批处理 [英] Deleting all registry keys containing or named "string" with Batch

查看:622
本文介绍了删除所有包含或命名为“字符串"的注册表项.与批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理批处理文件,以删除所有包含或命名为特定字符串的注册表项(是的,我知道这样做的危险),但是,我遇到了一些问题.

I am currently working on a batch file to delete all registry keys containing or named a specific string (yes I know the dangers of doing this), however, I'm having a few issues.

我尝试使用的代码是:

@echo off
set "key=HKEY_LOCAL_MACHINE\SOFT-EXT"
set "search=string"
for /f "delims=" %%a in ('reg query "%key%" /s^| findstr "%search%"') do reg delete "%key%" /v "%%~a"

这适用于注册表的非常特定的预定区域-对我而言,唯一的缺点是我的脚本需要解析ENTIRE SOFTWARE注册表配置单元,该配置单元是从外部驱动器加载的.当前发生的情况是该脚本将运行约20秒钟,而在命令提示符下没有任何返回.之后,我继续包含FINDSTR: Line 50300 too long的行,并在行号上增加了值.一些简短的阅读使我相信findstr只能管理这么多字节的数据,并且由于此注册表配置单元的巨大容量(每个大约80-100MB),我显然使它超载了.

This works for a very specific, predetermined area of the registry - the only drawback for me is that my script needs to parse the ENTIRE SOFTWARE registry hive, which is loaded from an external drive. What currently happens is the script will run for about ~20 seconds with no returns in the command prompt. After that, I get continues lines containing FINDSTR: Line 50300 too long, with an increasing value on the line number. Some brief reading leads me to believe that findstr can only manage so many bytes of data, and I am obviously overloading it due to the massive size of this registry hive (approx. 80-100MB each).

为解决此问题,我尝试将for循环更改为具有两个条件&&||的附加查询,但是,这也不起作用,因为偶尔包含该字符串的注册表项可能会被命名为HKLM\SOFTWARE\Classes\CSID\{###-###-###}\Example,其DWORD名称为Name,数据值为string.

To fix this issue, I attempted to change the for loop to have an additional query with two conditional &&s and ||, however, this doesn't work either, because occasionally the registry key containing the string might be named HKLM\SOFTWARE\Classes\CSID\{###-###-###}\Example, with a DWORD with the name of Name and the data value of string.

有什么方法可以处理批处理文件吗?我相信可以使用PowerShell,但是,我正在使用的环境不支持PowerShell或VB脚本.

Is there any way to do what I would like to do with a batch file? I believe it is possible to do with PowerShell, however, the environment I'm working in does not support PowerShell or VB scripting.

推荐答案

我不确定您想要什么:

您是否真的要删除问题标题中写在注册表 Regedit 中树左侧的注册表,或注册表 Regedit 右侧显示的?类型(REG_SZ?).

Do you really want to delete registry keys displayed on left side in tree in Regedit as written in title of question, or registry values of type ? (REG_SZ?) displayed on right side in Regedit.

我认为,您要删除类型为?的注册表值,这比删除注册表项要困难得多.

I think, you want to delete registry values of type ? which is much more difficult than deleting registry keys.

这是用于搜索注册表项的批处理代码,只需在注释的批处理代码的顶部用Action=Find列出它们,或者用Action=Delete删除它们即可.

Here is a batch code to search for registry keys and either just list them with Action=Find or additionally delete them with Action=Delete at top of the commented batch code.

强烈建议先使用此处发布的Action=Find运行此批处理代码,然后在找到的注册表项之前使用Action=Delete删除它们.

It is strongly recommended to run this batch code first with Action=Find as posted here and look on found registry keys before deleting them using Action=Delete.

@echo off
setlocal

rem Change value "Find" to "Delete" to really delete all found keys.
set "Action=Find"

rem Define the root key for the search.
set "RegKey=HKEY_LOCAL_MACHINE\SOFTWARE"

rem Define the string which must be found in name of a key to delete.
rem It should not contain characters interpreted by FINDSTR as regular
rem expression character, see help output on entering FINDSTR /? in a
rem command prompt window.
set "Search=ABCDEFGHIJKLM"

rem Check if specified registry key exists at all.
%SystemRoot%\System32\reg.exe query "%RegKey%" 1>nul 2>nul
if not errorlevel 1 goto RunSearch

echo.
echo Registry key "%RegKey%" not found.
goto EndBatch

:RunSearch
rem Exporting everything of defined root key to a temporary text file.
echo Exporting registry key "%RegKey%" ...
%SystemRoot%\System32\reg.exe query "%RegKey%" /s >"%TEMP%\RegExport.tmp" 2>nul

rem The backslash is the escape character in regular expressions. Therefore
rem it is necessary to escape this character in root registry key to get a
rem working regular expression search string as long as the root registry
rem key and the search string do not contain other characters with special
rem registry expression meaning.
set "RegKey=%RegKey:\=\\%"

rem Interesting are only lines in exported registry which contain the
rem search string in last key of a registry key path. In other words
rem the deletion of a key is always done only on root key containing in
rem name the search string and not also on all subkeys to improve speed.

if /I "%Action%"=="Delete" (
    echo Searching for keys containing "%Search%" and delete all found ...
) else (
    echo Searching for keys containing "%Search%" and list all found ...
)

rem The expression below works only correct if whether RegKey nor
rem Search contains characters with a regular expression meaning.
set "FoundCounter=0"
set "DeleteCounter=0"
for /f "delims=" %%K in ('%SystemRoot%\System32\findstr.exe /R "^%RegKey%.*%Search%[^\\]*$" "%TEMP%\RegExport.tmp" 2^>nul') do (
    echo %%K
    set /A FoundCounter+=1
    if /I "%Action%"=="Delete" (
        %SystemRoot%\System32\reg.exe delete "%%K" /f >nul
        if not errorlevel 1 set /A "DeleteCounter+=1"
    )
)
del "%TEMP%\RegExport.tmp"

set "FoundPlural="
if not %FoundCounter%==1 set "FoundPlural=s"
set "DeletePlural="
if not %DeleteCounter%==1 set "DeletePlural=s"

echo.
if /I "%Action%"=="Delete" (
    echo Deleted %DeleteCounter% key%DeletePlural% of %FoundCounter% key%FoundPlural% containing "%Search%".
) else (
    echo Found %FoundCounter% key%FoundPlural% containing "%Search%".
)

:EndBatch
endlocal
echo.
echo Exit with any key ...
pause >nul

这篇关于删除所有包含或命名为“字符串"的注册表项.与批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆