How to create a For loop to execute lines in the same .bat file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Docfxit
Posts: 51
Joined: 12 Nov 2015 12:42

How to create a For loop to execute lines in the same .bat file

#1 Post by Docfxit » 09 Feb 2020 22:54

I have a .bat file that looks like this:

Code: Select all

(SET _KEY=SOFTWARE\Rainmaker Research)
REG Delete "HKLM\%_KEY%" /V Config  /F >NUL
(SET _KEY=.1sc)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.2sc)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.3sc)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.4sc)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.5sc)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.6sc)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.7sc)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.8sc)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.sc_clx)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.sc_dct)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.sc_env)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.sc_lex)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.sc_ths)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.sckey)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=.screq)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=CLSID\{A5B27365-8341-0000-5792-C10604A0DE02})
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=scwkey)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=scwreq)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL
(SET _KEY=Spell Catcher Plus)
REG Delete "HKCR\%_KEY%" /V Config  /F >NUL

@CMD /k
I have more lines I will add. I would like to know how to write it so I don't have to repeat the REG Delete line every time.

Thank you,

Docfxit

Eureka!
Posts: 69
Joined: 25 Jul 2019 18:25

Re: How to create a For loop to execute lines in the same .bat file

#2 Post by Eureka! » 10 Feb 2020 02:03

Like your subject - "How to create a For loop to execute lines in the same .bat file" - already says:
Use a FOR loop. Type FOR /? for (a lot) more information.

An example:

Code: Select all


for %%k in (
    "HKLM\SOFTWARE\Rainmaker Research"
    "HKCR\.1sc"
    "HKCR\.2sc"
    "HKCR\.3sc"
    "HKCR\.4sc"
    "HKCR\.5sc"
) DO echo REG Delete "%%~k" /V Config  /F

Remove the echo before REG if the output is what you expected.

(although I doubt that there will be a Config entry under any of the HKCR keys ... )

Docfxit
Posts: 51
Joined: 12 Nov 2015 12:42

Re: How to create a For loop to execute lines in the same .bat file

#3 Post by Docfxit » 10 Feb 2020 13:41

You are brilliant. That is exactly what I need. Very nicely done.

Thank you very very much.

That's super.

Docfxit

Docfxit
Posts: 51
Joined: 12 Nov 2015 12:42

Re: How to create a For loop to execute lines in the same .bat file

#4 Post by Docfxit » 10 Feb 2020 15:32

I'm having a problem when I have a value that I need to delete.
This is what I have created:

Code: Select all

for %%k in (
  	"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs" /V "C:\\Windows\\system32\\SCHelp.dll"
) DO Echo REG Delete "%%~k" /F
It deletes all values and "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs"
Instead of only deleting the value "C:\Windows\system32\SCHelp.dll"

This code gives me ERROR: Invalid key name.

Code: Select all

for %%k in (
  	""HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs" /V "C:\\Windows\\system32\\SCHelp.dll""
) DO Echo REG Delete "%%~k" /F
This is what the registry looks like:
(NOTE: I have removed many values )

Code: Select all

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs]
"C:\\Windows\\system32\\vfpodbc.dll"=dword:00000001
"%CommonProgramFiles%\\System\\wab32.dll"=hex(0):01,00,00,00
"c:\\GenRad\\DiagSystem\\Launcher\\Launcher.exe"=dword:00000001
"C:\\Windows\\system32\\SCHelp.dll"=dword:00000001

Eureka!
Posts: 69
Joined: 25 Jul 2019 18:25

Re: How to create a For loop to execute lines in the same .bat file

#5 Post by Eureka! » 10 Feb 2020 16:16

Docfxit wrote:
10 Feb 2020 15:32

Code: Select all

for %%k in (
  	"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs" /V "C:\\Windows\\system32\\SCHelp.dll"
) DO Echo REG Delete "%%~k" /F
It deletes all values and "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs"
That is expected when you do it like this:
Between teh () are a list of items, separated by a space. In this case:
- "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs"
- /V
- "C:\\Windows\\system32\\SCHelp.dll"

The for loop will fill in these items one by one in the Echo REG Delete "%%~k" /F command, using the %%k for passing these items through to the command:

Echo REG Delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs" /F
Echo REG Delete "/V" /F
Echo REG Delete "C:\\Windows\\system32\\SCHelp.dll" /F

Another thing: you used \\ instead of \ :
"C:\\Windows\\system32\\SCHelp.dll" should be "C:\Windows\system32\SCHelp.dll".
(\\ is used when exporting from regedit to a .reg file)

If not sure, test with reg query:
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs" /v "C:\Windows\system32\SCHelp.dll"
- vs -
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs" /v "C:\\Windows\\system32\\SCHelp.dll"


Better syntax:

Code: Select all

@echo on
for %%k in (
  	"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs"
) DO Echo REG Delete "%%~k"  /V "C:\Windows\system32\SCHelp.dll" /F
Last edited by Eureka! on 10 Feb 2020 16:27, edited 1 time in total.

Docfxit
Posts: 51
Joined: 12 Nov 2015 12:42

Re: How to create a For loop to execute lines in the same .bat file

#6 Post by Docfxit » 10 Feb 2020 16:25

I wasn't clear. I removed all other lines in the For loop to make it simpler.
I really have a lot of entries in the For loop.
The /V "C:\\Windows\\system32\\SCHelp.dll" only belongs to one of the lines.

Code: Select all

for %%k in (
   "HKLM\SOFTWARE\Rainmaker Research"
    "HKCR\.1sc"
    "HKCR\.2sc"
    "HKCR\.3sc"
    "HKCR\.4sc"
    ""HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs" /V "C:\\Windows\\system32\\SCHelp.dll""
) DO Echo REG Delete "%%~k" /F 

Eureka!
Posts: 69
Joined: 25 Jul 2019 18:25

Re: How to create a For loop to execute lines in the same .bat file

#7 Post by Eureka! » 10 Feb 2020 16:38

Ah, now I see .. you used double double-quotes!
That will result in too many quotes here:
REG Delete ""HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs" /V "C:\\Windows\\system32\\SCHelp.dll"" /F

I would keep it simple: Keep the exceptions out of the FOR-loop, like this:

Code: Select all

for %%k in (
   "HKLM\SOFTWARE\Rainmaker Research"
    "HKCR\.1sc"
    "HKCR\.2sc"
    "HKCR\.3sc"
    "HKCR\.4sc"
) DO Echo REG Delete "%%~k" /F 

REG Delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs" /V "C:\Windows\system32\SCHelp.dll"

Another thing: use \ instead of \\. \\ is used when you export in regedit, not in the reg commands
(test it with reg query:
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SharedDLLs" /V "C:\\Windows\\system32\\SCHelp.dll"
)

Post Reply