.BAT - Retrieve "Data" from the WINDOWS Registry
Solved
micromega
Posted messages
141
Status
Membre
-
Micromega -
Micromega -
Hello,
I created an entry in the Windows registry that looks like this:
[HKEY_CLASSES_ROOT\TEST]
@="C:\\Program Files (x86)\\DOSSIER A SUPPRIMER"
On the other hand, I have a .bat file that wants to retrieve the data: "C:\\Program Files (x86)\\DOSSIER A SUPPRIMER" to directly delete the folder.
I want to use the registry, as it saves me from doing a long search across all drives.
How can I retrieve the data from the registry so that my .bat can use it?
This is beyond my skills...
Thank you for your help.
I created an entry in the Windows registry that looks like this:
[HKEY_CLASSES_ROOT\TEST]
@="C:\\Program Files (x86)\\DOSSIER A SUPPRIMER"
On the other hand, I have a .bat file that wants to retrieve the data: "C:\\Program Files (x86)\\DOSSIER A SUPPRIMER" to directly delete the folder.
I want to use the registry, as it saves me from doing a long search across all drives.
How can I retrieve the data from the registry so that my .bat can use it?
This is beyond my skills...
Thank you for your help.
14 réponses
Hi Dubcek,
First of all, thank you for helping me once again. You're always there to lend a hand, that's great!
"Reg Query" is indeed a piece of code that I was missing.
I did: Reg query "HKEY_CLASSES_ROOT\TEST"
and it found: C:\Program Files (x86)\DOSSER TO DELETE
I want to use this value, either to delete a folder (here "DOSSER TO DELETE"), or to launch an ".exe" which would have been identified in the same way.
I thought it was simple, so I wrote a bat code like this:
Reg query "HKEY_CLASSES_ROOT\TEST"
rd /s /q "DOSSER TO DELETE"
But I must be off track after all. I've searched the internet but I couldn't find anything that addresses a possible action after retrieving the path.
Any idea?
Thanks.
First of all, thank you for helping me once again. You're always there to lend a hand, that's great!
"Reg Query" is indeed a piece of code that I was missing.
I did: Reg query "HKEY_CLASSES_ROOT\TEST"
and it found: C:\Program Files (x86)\DOSSER TO DELETE
I want to use this value, either to delete a folder (here "DOSSER TO DELETE"), or to launch an ".exe" which would have been identified in the same way.
I thought it was simple, so I wrote a bat code like this:
Reg query "HKEY_CLASSES_ROOT\TEST"
rd /s /q "DOSSER TO DELETE"
But I must be off track after all. I've searched the internet but I couldn't find anything that addresses a possible action after retrieving the path.
Any idea?
Thanks.
You need to extract the name of the channel returned by the reg query. Try:
@echo off
for /F "tokens=3* delims=\" %%a in ('reg query HKEY_CLASSES_ROOT\TEST ^| findstr REG_') do (
echo the access path is %%a
set value=%%a
)
echo value=%value%
Hello,
When I launch, I get this message:
Error: Error: the system cannot find the specified Registry key or value.
value=
Press any key to continue...
I tried to fiddle around, but nothing conclusive...
:/
When I launch, I get this message:
Error: Error: the system cannot find the specified Registry key or value.
value=
Press any key to continue...
I tried to fiddle around, but nothing conclusive...
:/
The command
Reg query "HKEY_CLASSES_ROOT\TEST"returns the values, subkeys, and data associated with the specified registry key "HKEY_CLASSES_ROOT\TEST". If the key exists, it will display the information; if it does not exist, it will indicate that the system cannot find the file specified.
Hi Dubcek,
Actually, I made a mistake while copying the code.
The result is quite positive, here's what I get:
the access path is FOLDER TO DELETE
value=FOLDER TO DELETE
Press any key to continue...
Now, where and how can I insert a:
rd /s /q (to delete a folder)
or
call (to launch an .exe application contained in the folder found by the registry key)
??
Actually, I made a mistake while copying the code.
The result is quite positive, here's what I get:
the access path is FOLDER TO DELETE
value=FOLDER TO DELETE
Press any key to continue...
Now, where and how can I insert a:
rd /s /q (to delete a folder)
or
call (to launch an .exe application contained in the folder found by the registry key)
??
This is where I also tried to incorporate the "RD" or "Call", but I receive the message:
"The specified file was not found" (and this time I didn't make a mistake ;))
If instead I do:
echo valeur=%valeur%
it tells me:
the access path is FOLDER TO DELETE
valeur=FOLDER TO DELETE
If I only do:
Reg query "HKEY_CLASSES_ROOT\TEST"
I get: C:\Program Files (x86)\FOLDER TO DELETE
Here is the code I wrote following your instructions:
Does the "RD" value take into account the entire path provided by "reg query"?
We are really close to the goal now...
"The specified file was not found" (and this time I didn't make a mistake ;))
If instead I do:
echo valeur=%valeur%
it tells me:
the access path is FOLDER TO DELETE
valeur=FOLDER TO DELETE
If I only do:
Reg query "HKEY_CLASSES_ROOT\TEST"
I get: C:\Program Files (x86)\FOLDER TO DELETE
Here is the code I wrote following your instructions:
echo off
for /F "tokens=3* delims=\" %%a in ('reg query HKEY_CLASSES_ROOT\TEST ^| findstr REG_') do (
echo the access path is %%a
set valeur=%%a
)
rd /s /q "%valeur%"
pause
end
Does the "RD" value take into account the entire path provided by "reg query"?
We are really close to the goal now...
It is clear that the full path is needed, try
echo off
for /F %%a in ('reg query HKEY_CLASSES_ROOT\TEST ^| findstr REG_') do (
echo the access path is %%a
set value=%%a
)
echo rd /s /q "%value%"
pause
end
Tried, I get:
Error: Error: the system could not find the specified Registry key or value.
We will overcome...
:/
Error: Error: the system could not find the specified Registry key or value.
We will overcome...
:/
If I simply do:
('reg query HKEY_CLASSES_ROOT\TEST ^| findstr REG_')
I get:
D:\Desktop>('reg query HKEY_CLASSES_ROOT\OKOK | findstr REG_')
''reg' is not recognized as an internal or external command, an executable program or a batch file.
('reg query HKEY_CLASSES_ROOT\TEST ^| findstr REG_')
I get:
D:\Desktop>('reg query HKEY_CLASSES_ROOT\OKOK | findstr REG_')
''reg' is not recognized as an internal or external command, an executable program or a batch file.
I am working in ".bat" because I would like to have the result as a .bat file.
If I execute (in .bat) the last code you sent me, everything seems fine, it successfully finds the correct path from the registry:
the access path is (by default) REG_SZ C:\Program Files (x86)\FOLDER TO DELETE
Press any key to continue...
I tried adding a RD /q /s afterwards in various attempts, but it didn't work.
What is certain is that we retrieve the correct path from the registry.
And what blocks is reusing this path to delete the folder...
If I execute (in .bat) the last code you sent me, everything seems fine, it successfully finds the correct path from the registry:
the access path is (by default) REG_SZ C:\Program Files (x86)\FOLDER TO DELETE
Press any key to continue...
I tried adding a RD /q /s afterwards in various attempts, but it didn't work.
What is certain is that we retrieve the correct path from the registry.
And what blocks is reusing this path to delete the folder...
Hello,
I would like to reopen this topic, as after using this .bat command which works perfectly, I encountered a problem:
If from a defined registry key, I do:
For /F "tokens=3,*" %%a in ('reg query HKEY_CLASSES_ROOT\TEST ^| findstr REG_') do (echo rd /s /q "%%b")
I will get, for example:
C:\\Program Files (x86)\\FOLDER 1\\FOLDER2
The command will delete "FOLDER 2", but I want to delete the root directly, so: FOLDER1 (which includes FOLDER 2 and everything else inside)
My question is, how can I have a rollback action with this command?
Thank you for your help.
I would like to reopen this topic, as after using this .bat command which works perfectly, I encountered a problem:
If from a defined registry key, I do:
For /F "tokens=3,*" %%a in ('reg query HKEY_CLASSES_ROOT\TEST ^| findstr REG_') do (echo rd /s /q "%%b")
I will get, for example:
C:\\Program Files (x86)\\FOLDER 1\\FOLDER2
The command will delete "FOLDER 2", but I want to delete the root directly, so: FOLDER1 (which includes FOLDER 2 and everything else inside)
My question is, how can I have a rollback action with this command?
Thank you for your help.
Hello Dubcek,
Just one last question about the code.
I'm using this code that works really well:
@Echo off
setlocal enableDelayedExpansion
For /F "tokens=3,*" %%a in ('reg query "REGISTRY KEY" ^| findstr REG_') do (
for /f "delims=" %%c in ("%%b") do (
set L=%%~dc
set ff=%%~pc
set ff=!ff:~0,-1!
for /f "delims=" %%d in ("!ff!") do (
rd /s/q "!L!%%~pd" >NUL 2>&1
)
)
)
If I want to go forward in the path given by the registry, I do:
"!L!%%~pd\FOLDER 3\FOLDER 4" and it works perfectly without any issues.
But how can I reference a folder that is behind?
For example, if the path given by the registry is:
c:\TEST\FOLDER 1\FOLDER 2
How can I reference "FOLDER 1" or "TEST"?
Thank you.
Just one last question about the code.
I'm using this code that works really well:
@Echo off
setlocal enableDelayedExpansion
For /F "tokens=3,*" %%a in ('reg query "REGISTRY KEY" ^| findstr REG_') do (
for /f "delims=" %%c in ("%%b") do (
set L=%%~dc
set ff=%%~pc
set ff=!ff:~0,-1!
for /f "delims=" %%d in ("!ff!") do (
rd /s/q "!L!%%~pd" >NUL 2>&1
)
)
)
If I want to go forward in the path given by the registry, I do:
"!L!%%~pd\FOLDER 3\FOLDER 4" and it works perfectly without any issues.
But how can I reference a folder that is behind?
For example, if the path given by the registry is:
c:\TEST\FOLDER 1\FOLDER 2
How can I reference "FOLDER 1" or "TEST"?
Thank you.
Like this ? :
@Echo off
setlocal enableDelayedExpansion
For /F "tokens=3,*" %%a in ('reg query "REGISTER KEY" ^| findstr REG_') do (
for /f "delims=" %%c in ("%%b") do (
set L=%%~dc
set ff=%%~pc
set ff=!ff:~0,-1!
for /f "delims=" %%d in ("!ff!") do (
C:> set c=C:\TEST\FOLDER 1\FOLDER 2
C:> for /F "tokens=1-16 delims=:\" %a in ("%c%") do @echo %b
TEST
C:> for /F "tokens=1-16 delims=:\" %a in ("%c%") do @echo %c
FOLDER 1
C:> for /F "tokens=1-16 delims=:\" %a in ("%c%") do @echo %d
FOLDER 2
rd /s/q "!L!%%~pd" >NUL 2>&1
)
)
)
@Echo off
setlocal enableDelayedExpansion
For /F "tokens=3,*" %%a in ('reg query "REGISTER KEY" ^| findstr REG_') do (
for /f "delims=" %%c in ("%%b") do (
set L=%%~dc
set ff=%%~pc
set ff=!ff:~0,-1!
for /f "delims=" %%d in ("!ff!") do (
C:> set c=C:\TEST\FOLDER 1\FOLDER 2
C:> for /F "tokens=1-16 delims=:\" %a in ("%c%") do @echo %b
TEST
C:> for /F "tokens=1-16 delims=:\" %a in ("%c%") do @echo %c
FOLDER 1
C:> for /F "tokens=1-16 delims=:\" %a in ("%c%") do @echo %d
FOLDER 2
rd /s/q "!L!%%~pd" >NUL 2>&1
)
)
)
Hello Dubcek,
I understand the method well, but I'm still stuck on the syntax.
I did:
@Echo off
setlocal enableDelayedExpansion
For /F "tokens=3,*" %%a in ('reg query "REGISTRY KEY" ^| findstr REG_') do (
for /F "tokens=1-16 delims=:\" %%q in ("%%b") do (
echo rd /s/q "%%q"
)
)
pause
I'm getting the response: rd /s/q "C"
Is that normal or did I make a mistake in the code syntax again?
Thank you.
I understand the method well, but I'm still stuck on the syntax.
I did:
@Echo off
setlocal enableDelayedExpansion
For /F "tokens=3,*" %%a in ('reg query "REGISTRY KEY" ^| findstr REG_') do (
for /F "tokens=1-16 delims=:\" %%q in ("%%b") do (
echo rd /s/q "%%q"
)
)
pause
I'm getting the response: rd /s/q "C"
Is that normal or did I make a mistake in the code syntax again?
Thank you.