Home > database >  Call a executable to execute command in all folders cmd or powershell
Call a executable to execute command in all folders cmd or powershell

Time:03-24

I am using this .bat command to my tool. rip_images.exe is a tool to merge images

rip_images.exe -c 1 00000001.jpg 00000002.jpg 00000003.jpg 00000004.jpg -o out.png
rip_images.exe -c 1 00000001.bmp 00000002.bmp 00000003.bmp 00000004.bmp -o out.bmp

just work if i put these files in the same folder of the executable.

i need a command that search for .jpg and bmp in folders and execute the .exe in each folder, give the merged files in each folder separetely. cmd or powershell. I have try something like that in powershell

Powershell "Get-ChildItem -File -Filter *bmp |ForEach-Object 
{.\rip_image.exe -c 1 $_.FullName}"

but it do in file by file, need something like save variables to put in arguments maybe for /r?

example:

 rip_images.exe -c 1 var1 var2 var3 var4 -o out.bmp

thank you for attention.

CodePudding user response:

This is how you can do it using exclusively PowerShell, if you attempt to run this from CMD by calling powershell.exe this will most likely fail. You can however store this as a .ps1 file and the use powershell -File path\to\script.ps1.

You can use Group-Object to group the files by their extension and then pass the FullName of that group of objects to your binary:

Get-ChildItem -Directory | ForEach-Object {
    Get-ChildItem $_ -File |
        Where-Object { $_.Extension -in '.bmp', '.png' } |
        Group-Object Extension | ForEach-Object {
            # $_.Name is the extension here
            $folderName = $_.Group.Directory[0]
            .\rip_image.exe -c 1 $_.Group.FullName -o "$folderName out$($_.Name)"
        }
    }
  • Related