.bat Wildcard Question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
konjis1
Posts: 4
Joined: 02 Jul 2014 13:43

.bat Wildcard Question

#1 Post by konjis1 » 02 Jul 2014 14:24

I am new to creating .bat files and i am trying to figure out how to do a delete on the registry. I am not sure if you can use wildcards in a directory path and not sure how to search for pieces of a data value.

We have an application that writes to the registry and we want to delete every registry key value with "TM1" in it. The issue we are running into is if the values resides on HKLM\SOFTWARE how to write the command to delete any data value that includes "TM1".

Here is what i tried to run with no success:

REG DELETE HKLM\SOFTWARE\* /v TM1

Is this correct?

Thank you in advance! :mrgreen:

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: .bat Wildcard Question

#2 Post by Yury » 04 Jul 2014 18:09

Code: Select all

@echo off
for /f "delims=" %%i in ('
 mshta "javascript:new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write('\t')&close()"
') do (
 set TAB=%%i
 )
for /f "delims=" %%i in ('
 reg query "HKLM\SOFTWARE" /v /f "TM1" /s^| findstr /bi "HKEY_LOCAL_MACHINE\\SOFTWARE\\"
') do (
 for /f "delims=" %%j in ('
  reg query "%%i"^| findstr "\<TM1\>"^| findstr /biv "HKEY_LOCAL_MACHINE\\SOFTWARE\\"
 ') do (
  set var=%%j
  call set "var=%%var:    =%%"
  call set "var=%%var:%TAB%=%%"
  for /f "delims=$" %%k in ('
   call set /p"=%%var:REG_=$%%"^<nul
  ') do (
   reg delete "%%i" /v "%%k" /f
   )
  )
 )
exit /b

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: .bat Wildcard Question

#3 Post by Compo » 05 Jul 2014 12:27

If the search term was for registry keys then you could use this from the Run Box (Vista or later)!

Code: Select all

Cmd /C @For /f "Tokens=1* Delims=_" %A In ('Reg Query HKLM\software /K /S /F TM1') Do @If %A Equ HKEY Reg Delete "%A_%B" /F >Nul


<EDIT>
If you can be sure that the Registry values containing your search string do not contain spaces then the following batch code may be sufficient for your needs. (Also Vista or later)

Code: Select all

@Echo off & Setlocal EnableDelayedExpansion
For /f "Tokens=1* Delims=_ " %%A In (
   'Reg Query HKLM\software /V /S /F TM1 /T REG_SZ') Do (
   If %%A Equ HKEY (Set _=Reg Delete "%%A_%%B") Else (If %%A NEq End (
          Set _=!_! /V %%A /F) & !_! >Nul))
</EDIT>

I would suggest that you do not blindly delete registry keys/values where the search term is so short

Post Reply