0

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?

preachers
  • 373
  • 1
  • 5
  • 15
  • The bottommost line should work, assuming `$output_path` is populated, forming a correct path to file list as a result. Grab stdout and stderr of that process like here https://stackoverflow.com/questions/8761888/capturing-standard-out-and-error-with-start-process and debug what data does reaper.exe receive in its command line. Also there's a chance that reaper.exe is not on the path so you need to provide full path to executable. – Vesper Dec 25 '18 at 08:58
  • @Vesper The `$output_path` is ok, I assigned the path before. The reason I run Reaper without its path is that I've set it to my system's environment variable. I'm going to test the stdout and stderr with your suggestion, thanks. – preachers Dec 25 '18 at 09:06
  • @Vesper I've tried the code with `filename` and `arguments` being replaced by Reaper and its own parameters, the result however, is nothing. I got `stdout: stderr: exit code: + 0` – preachers Dec 25 '18 at 09:23
  • Probably then profile the app with Procmon or similar tool, it seems to not provide debug logs in standard IO streams, maybe you'll discover that it's not lanched with correct parameter set, either with encoding or with "extra" spaces in path resulting in the path being split into several arguments. (BTW it's always nice to try eliminating spaces from any passed path when dealing with an unknown app, who knows which API it uses to get path. Same with non-ASCII characters in path) – Vesper Dec 25 '18 at 09:33
  • Are you sure that Reaper doesn't start another process and then exits the first one? That could explain why PS doesn't wait for it. – montonero Dec 25 '18 at 14:59
  • @montonero Yes, it is! It would fire up a new window, process the audio file that is given to it and then close the window and exit. – preachers Dec 26 '18 at 03:00

2 Answers2

1

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("- ", "")}
preachers
  • 373
  • 1
  • 5
  • 15
0

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 }
JonR85
  • 700
  • 4
  • 12
  • First, the file generated by Reaper is not the .txt, this is the filelist with parameters required by Reaper. It actually outputs a .wav file in another location. Second, the next statement is to change the file name generated by Reaper, there is a problem if you use a `Test-Path` command that the file would be there at the beginning of the process, the process, however, would take a while to finish. – preachers Dec 26 '18 at 03:11
  • Second, when the file is detected to exist, the `Rename-Item` command would change its name at once, this would lead the results either the file being processed by Reaper is damaged of it doesn' have the permission to change the file's name because it is opened by another process. – preachers Dec 26 '18 at 03:15
  • 1
    There could be another approach by finding a process with your main process as a parent one. These could be helpful https://stackoverflow.com/questions/4762982/powershell-get-process-id-of-called-application https://stackoverflow.com/questions/33911332/powershell-how-to-get-the-parentprocessid-by-the-processid – montonero Dec 26 '18 at 06:59