Run a external program with parameters and wait for it to end in Powershell

2.9k Views Asked by At

Actually, I've found so many solutions to this question, but none works. The program I'd like to run in Powershell is Reaper - a Digital Audio Workstation, and I'm going to use its command line tool for batch-processing audio files within a PS script. The code related to Reaper is as below:

reaper -batchconvert $output_path\audio\Reaper_filelist.txt

I'm going to use the Start-Process with -wait parameter to allow my script to wait for it to end then go on to the next line of code which is a Rename-Item function.

ls $processed_audio_path | Rename-Item -NewName {$_.name.Replace("- ", "")}

If PS doesn't wait for the process to finish, then the next line would throw an error, something like "no such file was found in the directory".

The suggestions I've found are here, here, and here. But none of those work. The problem is that Reaper doesn't accept the argument to be added separately as:

$exe = "reaper"
$arguments = "-batchconvert $output_path\audio\Reaper_filelist.txt"
Start-Process -filepath $exe -argumentlist $arguments -wait

or:

Start-Process -filepath reaper -argumentlist "-batchconvert $output_path\audio\Reaper_filelist.txt" -Wait

or:

Start-Process -filepath reaper -argumentlist @("-batchconvert", "$output_path\audio\Reaper_filelist.txt") -Wait

It can only work without a problem as a whole block like the first code line above. So what can I do now?

4

There are 4 best solutions below

3
On

As the one comment mentions, it could be that the process starts another process, causing powershell to move along in the script. If that's the case, you could have a while statement to wait for the file to be created.

while (!(Test-Path "$output_path\audio\Reaper_filelist.txt")) { Start-Sleep 10 }
1
On

I've found a solution to this problem.
I think I need to describe more context about this. I always have Reaper launched with my Windows in the background, when the script calls the BatchConvert function of Reaper, it will fire up another instance of Reaper, so I got 2 instances of it when converting audio files. This - the instances of Reaper - could be a reliable condition to restrict the following code. I found something useful from here and here.

Finally, I got my code like this and it works:

# Batch converting through Reaper FX Chain
reaper -batchconvert $output_path\audio\Reaper_filelist.txt
while (@(Get-Process reaper).Count -eq 2){
    Start-Sleep -Milliseconds 500
    }
# Correct the Wrong file name produced by Reaper
ls $processed_audio_path | Rename-Item -NewName {$_.name.Replace("- ", "")}
2
On

I had to make a script wait for 7zip to finish extracting/archieving. This worked for me:

start-process "$($homedir)\7za.exe" -argumentlist $argL

while((get-process -ea Ignore 7za)){}

The script is trapped between two decorative curly braces and resumes execution only after 7za is finished with its task.

Greetings Johann

0
On

I verified that this will start Reaper, converting any files specified in the file .\list.txt from the current directory, and wait for it to exit

Start-Process 'C:\Program Files\REAPER (x64)\reaper.exe' -ArgumentList '-batchconvert .\list.txt' -Wait

And if you'd like to verify using a freely available sample file

Invoke-WebRequest `
  https://download.samplelib.com/wav/sample-15s.wav `
  -OutFile C:\Users\Public\Downloads\sample-15s.wav

(1..20) | foreach {# Specify the sample to be converted 20 times
  'C:\Users\Public\Downloads\sample-15s.wav' |
    Out-File C:\Users\Public\downloads\list.txt -Append utf8
}

Start-Process `
  'C:\Program Files\REAPER (x64)\reaper.exe' `
  -ArgumentList '-batchconvert c:\Users\Public\Downloads\list.txt' -Wait

Notice however, that my example had to specify that the list.txt file should be in utf8 format for reaper to be able to process it.