?
Solved

find and delete registry strings in keys batch file

Posted on 2014-04-11
8
Medium Priority
?
8,139 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 93

Expert Comment

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

Accepted Solution

by:
oBdA earned 1000 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 59

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:jo80ge121
ID: 39996628
both of them worked.  thank you!
0
 
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 59

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 59

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Tech giants such as Amazon and Google have sold Alexa and Echo to such an extent that they have become household names. And soon they are expected to be used by commoners in their homes, ordering takeout, picking out a song, answering trivia questio…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

840 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