Solved

find and delete registry strings in keys batch file

Posted on 2014-04-11
8
6,452 Views
Last Modified: 2014-11-05
I have a need to delete any strings in a certain registry key location that has the word redirect in it.  Batch file is preferable but VB is welcomed.  
example;
reg key location
hkcu\software\Microsoft\windows NT\devices\
string value (any string with the below in the value)
redirect
0
Comment
Question by:jo80ge121
8 Comments
 
LVL 91

Expert Comment

by:nobus
ID: 39995732
i use regseeker for that : http://www.hoverdesk.net/
0
 
LVL 83

Accepted Solution

by:
oBdA earned 250 total points
ID: 39995921
Try the script below; it's currentyl in test mode and will only display the "reg delete" command(s) it would normally run, so that you can test it. To run it for real, remove the uppercase ECHO in line 7. The correct registry location is actually "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices", btw.
@echo off
setlocal enabledelayedexpansion
set Key=HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices
set Find=*redirect*
for /f "delims=" %%a in ('reg.exe query "%Key%" /v "%Find%" ^| find "REG_SZ"') do (
	call :GetValueName Value "%%a"
	ECHO reg.exe delete "%Key%" /v "!Value!" /f
)	

goto :eof
:GetValueName
set Return=
for %%a in (%~2) do (
	if "%%a"=="REG_SZ" (
		set %1=!Return:~1!&goto :eof
	) else (
		set Return=!Return! %%a
	)
)
goto :eof

Open in new window

0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 39996146
Building off oBdA answer to handle the leading white spaces in the value names, and elliminate the subroutine call (it's really hard to beat oBdA to these questions, too fast! :-] ) here's a slightly different but effective approach.

@echo off
setlocal EnableDelayedExpansion

REM Define registry location and pattern to look for
set "Key=HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices"
set "Find=*redirect*"

REM locate the registry values we are interested in deleting
for /f "delims=" %%a in ('reg.exe query "%Key%" /v "%Find%" ^| find "REG_SZ"') do (
    REM Trim off leading spaces from value name
    for /f "tokens=* delims= " %%b in ("%%a") do set "Name=%%b"
    REM Break apart REG QUERY output at REG_SZ literal
    set "Name=!Name:    REG_SZ=@!"
    for /f "delims=@" %%b in ("!Name!") do (
        REM Delete this registry value
        ECHO reg.exe delete "%Key%" /v "%%b" /f
        echo Deleted: "%%b"
    )
)

Open in new window

~bp
0
 

Author Closing Comment

by:jo80ge121
ID: 39996628
both of them worked.  thank you!
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:mcburn13
ID: 40422200
not working for me   the Find variable *s mean that it's wildcard before and after the term right?  Anyway just getting an error even on running the exact string value:
C:\Scripts>reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "\\test\test" | find "REG_SZ"
ERROR: The system was unable to find the specified registry key or value.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40422750
Can you do the following at a command prompt and post the results.  Based on what you described it does not seem that the KEY named "\\test\test" exists, which I would not expect it to.  The key names are typically reserved works / names like "AppData", "Desktop", "Start Menu", etc.

Anyway, you can check this by the following:

reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"

and see what keys are listed.

~bp
0
 
LVL 1

Expert Comment

by:mcburn13
ID: 40423949
I was actually able to get it going with this syntax:
SET KEY=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
SET VALUE=\\OLDSERVERNAME*
for /F "tokens=1,*" %%a in ('REG QUERY "%KEY%" ^| findstr /I "%VALUE%"') do (REG DELETE "%KEY%" /v %%a /f)

I had to specify the \\ in the UNC path it didn't like a preceding wildcard in the variable.  BUT I would like to figure out how to get that preceding wildcard working for future situations (doesn't apply here because this string in this instance will always start with \\OLDSERVERNAME)
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40425437
The leading asterisk definitely works, as in this test I just did:

C:\> reg.exe query "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices" /v "*envy*"

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices
    HP ENVY 120 series (Network)    REG_SZ    winspool,Ne01:

~bp
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
OfficeMate Freezes on login or does not load after login credentials are input.
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now