How to find all .bat files and execute them one by one?

I'm still very new to PowerShell and need some help.

I have some .bat files in a folder named: c:\scripts\run\ and I want to run them one by one, but I don’t know how much I have, it changes from time to time.

So, I want to run a loop with foreach as follows:

 foreach ($file in get-childitem c:\scripts\run | where {$_.extension -eq ".bat"}) 

But I do not know how to run them now. I know that I can run them 1 on 1 as follows:

 ./run1.bat ./run2.bat ./run3.bat 

But how to implement this? Thanks!!

+4
source share
2 answers

Try the following:

 Get-Childitem -Path c:\scripts\run -Filter *.bat | % {& $_.FullName} 
+10
source

you can use

 & $file.FullName 

inside your cycle.

I would probably just use a pipeline, but instead of an explicit foreach :

 Get-ChildItem C:\scripts\run -Filter *.bat | ForEach-Object { & $_.FullName } 

If you want to perform additional checks after running each batch file:

 gci C:\scripts\run -fi *.bat | % { & $_.FullName if (Test-Path C:\scripts\run\blah.log) { ... } } 
+3
source

Source: https://habr.com/ru/post/1494531/


All Articles