.BAT - Retrieve "Data" from the WINDOWS Registry

Solved
micromega Posted messages 141 Status Membre -  
 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.

14 réponses

dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
hello
with the reg command, see:
reg query /?
0
micromega Posted messages 141 Status Membre 1
 
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.
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
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%
0
Micromega
 
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...

:/
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
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.
0
Micromega
 
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)
??
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
remplacer
 echo value=%value%
par
 rd /s/q "%value%"
0
Micromega
 
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:

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...
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
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
0
Micromega
 
Tried, I get:

Error: Error: the system could not find the specified Registry key or value.

We will overcome...

:/
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
('reg query HKEY_CLASSES_ROOT\TEST ^| findstr REG_')
0
Micromega
 
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.
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
This command only works with FOR (with the ('
does this command return an error?
for /F %%a in ('reg query HKEY_CLASSES_ROOT\TEST ^| findstr REG_') do (
echo the access path is %%a
)
0
Micromega
 
Hello,

I did:


For /F %a in ('reg query HKEY_CLASSES_ROOT\TEST | findstr REG_') do (e
cho the path is %a )


And I get:

(echo the path is (by )
the path is (by


It finds a path that is "By", does that correspond to something in .bat?
Anyway, at least it responds to something, there is no error.
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
in a .bat or interactively?
and this in a bat
@echo off
For /F "delims=" %%a in ('reg query HKEY_CLASSES_ROOT\TEST ^| findstr REG_') do (echo the access path is %%a )

    
0
Micromega
 
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...
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
In post #8, you write
I get: C:\Program Files (x86)\FOLDER TO DELETE
no REG...
what is the exact line
0
micromega Posted messages 141 Status Membre 1
 
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.
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
tester en remplaçant %%b par %%~pb
C:> for /f "delims=" %b in ("C:\\Program Files (x86)\\DOSSIER 1\\DOSSIER2") do @echo %~pb
\Program Files (x86)\DOSSIER 1\
C:>
0
micromega Posted messages 141 Status Membre 1
 
Once again hello to you Dubcek and thank you.

I did:

For /F "tokens=3,*" %%a in ('reg query "HKEY_CLASSES_ROOT\TEST" ^| findstr REG_') do (rd /s /q "%%~pb")

It seems to have worked (I'm not quite sure yet), but I would need to go back once more, what should I add to go back?

Thank you
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
A method, we remove the trailing \ and loop through a FOR
C:> cc
\a\b\

C:> type cc.bat
@echo off
setlocal enableDelayedExpansion
set f=\a\b\c\d
for /f "delims=" %%b in ("%f%") do (
set ff=%%~pb
set ff=!ff:~0,-1!
for /f "delims=" %%c in ("!ff!") do (
echo %%~pc
)
)
0
Micromega
 
Sorry, but I didn't quite understand... :s

Is there no way from this:

For /F "tokens=3,*" %%a in ('reg query "HKEY_CLASSES_ROOT\TEST" ^| findstr REG_') do (rd /s /q "%%~pb")

to have something simpler to refer to folders prior to the folder designated by the registry key?

Thank you :)
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
So with C:\\Program Files (x86)\\DOSSIER 1\\DOSSIER2 you want to retrieve C:\\Program Files (x86)?
0
micromega Posted messages 141 Status Membre 1
 
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.
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
Put in a variable : \TEST or \FOLDER 1 or go inside?
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
```html with a loop like this
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

```
0
micromega Posted messages 141 Status Membre 1
 
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

)
)
)
0
dubcek Posted messages 18702 Registration date   Status Contributeur Last intervention   5 657
 
I was showing an interactive example on my C: drive
if reg query returns C:\TEST\FOLDER 1\FOLDER 2
then
For /F "tokens=3,*" %%a in ('reg query "REGISTRY KEY" ^| findstr REG_') do (
for /F "tokens=1-16 delims=:\" %%p in ("%%b") do (

%%p contains C, %%q TEST, %%r FOLDER 1, %%s FOLDER 2
0
Micromega
 
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.
0