I am trying to iterate all the registry key in order to find (contains) and delete jre1.5.0_14 values. Is there a way to do it?
The code below just finds jre1.5.0_14 under a specific key! I do want to iterate all of the keys. By the way if clause gets if it is equal to jre1.5.0_14, but it should be if it contains jre1.5.0_14.
Thanks in advance.
Best Regards.
@echo off
setlocal
set KEY_NAME="HKEY_CURRENT_USER\Software\Microsoft\Notepad"
set VALUE_NAME=jre1.5.0_14
FOR /F "skip=2 tokens=3" %%A IN ('REG QUERY %KEY_NAME%') DO if "%%A"=="%VALUE_NAME%" (
@echo %%A
)
I am posting this per your last comment. You (probably) need to go through all the keys, and you don't want /d at the end.
If you know the string is in a specific key, use this (which I think is what you were originally after):
For /f %%a in ('reg query HKCR /f jre1.5.0_14') do (
rem do deletion here
echo %%a &&pause
)
Use the following to look through all the keys. It runs the query on each key and if "hkey" is found in the output (indicating the query turned up something) it writes the line--the location--to regout.txt. The second for loop goes through each line of the file, where you can do a reg delete command.
@echo off
SETLOCAL EnableDelayedExpansion
del regout.txt >NUL 2>&1
for %%a in (HKCU, HKLM, HKU, HKCR) do REG QUERY %%a /f jre1.5.0_14 | FIND /i "hkey" >>regout.txt
for /f %%a in (regout.txt) do (
rem do deletion here
echo %%a &&pause
)
exit
If the file is empty the string wasn't found and you should run regedit in Windows again to confirm it's there.
Edit: Attempt to resolve "The process can not access the file because the file is being used by another process . . .
Try this code: It uses different file names and creates a tmp file with query output, then copies it to txt. The new txt file should definitely be unlocked.
@echo off
SETLOCAL EnableDelayedExpansion
del regout.txt >Nul 2>&1
del regout.tmp >Nul 2>&1
del output.txt >Nul 2>&1
for %%a in (HKCU, HKLM, HKU, HKCR) do REG QUERY %%a /f jre1.5.0_14 | FIND /i "hkey" >>output.tmp
copy output.tmp output.txt /y
for /f %%a in (output.txt) do (
rem do deletion here
echo %%a &&pause
del output.tmp
)
exit