Run ALL .reg, .ps1, and .bat files in a folder.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Run ALL .reg, .ps1, and .bat files in a folder.

#1 Post by PAB » 11 Mar 2020 14:21

I have a folder with .reg, .ps1, and .bat files [about 80 files in total].

I am trying to put together a Batch Script to run ALL of the files one after the other in SILENT mode within the Implement Folder.
I have created a Batch Script for each . . .

Code: Select all

@echo off
Set Folder=C:\Implement
For %%i In (%Folder%\*.reg) Do (regedit /s %%i)
echo.
echo REG Implementation Completed Successfully!
Pause
EXIT

Code: Select all

@echo off
Set Folder=C:\Implement
Set PowerShellExe=%windir%\System32\WindowsPowerShell\v1.0\powershell.exe
For %%i In (%Folder%\*.ps1) Do (%PowerShellExe% -NoProfile -ExecutionPolicy Bypass -Command %cd%\%%i)
echo.
echo PS Implementation Completed Successfully!
Pause
EXIT

Code: Select all

@echo off
Set Folder=C:\Implement
Set cmdExe=%windir%\System32\cmd.exe
For %%i In (%Folder%\*.bat) Do (%cmdExe% \%%i)
echo.
echo CMD Implementation Completed Successfully!
Pause
EXIT
Can anyone see if there is anything wrong with the code above before I run them please?

EDIT:

I found this bit of PowerShell code . . .

Code: Select all

Get-ChildItem '.\*.reg' | ForEach{
    $Result = Start-Process Reg -ArgumentList Import, $_ -Wait -PassThru
    echo ( '{0} - {1}' -f $Result.ExitCode, $_.Name)
}
Is this something that can be adapted, and if so, how please?

Thanks in advance.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Run ALL .reg, .ps1, and .bat files in a folder.

#2 Post by Squashman » 11 Mar 2020 15:07

Yes. Running a batch file from a batch file, releases control to the batch file being executed and does not return to the primary process. If you are trying to execute multiple batch files then you need to use the CALL command to execute them.

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Run ALL .reg, .ps1, and .bat files in a folder.

#3 Post by PAB » 12 Mar 2020 05:34

Thanks for the reply Squashman,
Squashman wrote:
11 Mar 2020 15:07
Yes. Running a batch file from a batch file, releases control to the batch file being executed and does not return to the primary process. If you are trying to execute multiple batch files then you need to use the CALL command to execute them.
I am not a programmer by any means or anything like that, I am just a keen user!

Thinking about what I want to do, I can live with just running the .reg files in the folder with a script because there are 85 of them. I know a little bit about batch scripts but I know even less about PowerShell scripts.

I will investigate and see if I can work it out and then post back.

Thanks in advance.

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

Re: Run ALL .reg, .ps1, and .bat files in a folder.

#4 Post by Eureka! » 12 Mar 2020 10:57

If one or more of your 80 filenames contains spaces, things will go wrong.
Same goes for your %Folder%: if it contains one or more spaces, things go wrong.

For example, in your .reg routine, replace

Code: Select all

For %%i In (%Folder%\*.reg) Do (regedit /s %%i)
with:

Code: Select all

For %%i In ("%Folder%\*.reg") Do (regedit /s "%%i")


Extra suggestion: Replace it with the following to test and see if the resulting output looks OK:

Code: Select all

For %%i In ("%Folder%\*.reg") Do (echo regedit /s "%%i")

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Run ALL .reg, .ps1, and .bat files in a folder.

#5 Post by PAB » 12 Mar 2020 12:30

Thanks for the reply Eureka!,
Eureka! wrote:
12 Mar 2020 10:57
Extra suggestion: Replace it with the following to test and see if the resulting output looks OK:

Code: Select all

For %%i In ("%Folder%\*.reg") Do (echo regedit /s "%%i")
None of the files contain spaces, only underscores, and the folder doesn't have any spaces either.

I will have to try this in the morning now!

So if the code above runs OK, how would I get it to output each .reg file as it is being processed when I run the actual script please?

Would I use something like this . . .

Code: Select all

For %%i In ("%Folder%\*.reg") Do (regedit /s "%%i") echo "%%i"
OR, something like this . . .

Code: Select all

For %%i In ("%Folder%\*.reg") Do echo (regedit /s "%%i")
Thanks in advance.

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

Re: Run ALL .reg, .ps1, and .bat files in a folder.

#6 Post by Eureka! » 13 Mar 2020 05:43

Something like this should take care of that:

Code: Select all

For %%i In ("%Folder%\*.reg") Do (
   echo Processing %%i
   regedit /s "%%i"
)

Post Reply