2

I would like to make a PowerShell script trigger other scripts based on the existence of *.mp4 files in subfolders of a parent folder.

The question is the following: I have a folder called "Cut" inside that folder I have other folders that follow the logic Model 01, Model 02, Model 03...

Inside each Model XX folder I have a .ps1 script that I would like to be triggered every time there is a .mp4 file inside it.

The main point is that this script would have to be called in a new instance, not inside the main script.

What's the best way to do this?

What I've come up with so far is this:

Get-ChildItem -Path "$env:USERPROFILE\Desktop\Cut" -Recurse -Filter "*.mp4" -ea 0 | ForEach-Object {
    Start-Process powershell ".\ffmpeg.ps1"
    }
Tyrone Hirt
  • 341
  • 1
  • 8
  • “What’s the best way” is too broad and is likely to bring opinions as answers. What’s the specific issue you’re trying to solve at the moment? – Doug Maurer Dec 17 '22 at 19:30
  • I specifically need to trigger these scripts if there are videos to be converted, if there is no `.mp4` file it is not to trigger the `ffmpeg.ps1` script. The point is that the main automation places videos in these folders according to another rule, and I need them to be converted without stopping the main automation. I also wouldn't want to create an `If` for each of those folders, that would make the code huge. – Tyrone Hirt Dec 17 '22 at 19:38

1 Answers1

1

I suggest inverting the logic by searching for all ffmpeg.ps1 files, and filtering them by whether at least one *.mp4 is present alongside each:

Get-ChildItem -Path $env:USERPROFILE\Desktop\Cut -Recurse -Filter ffmpeg.ps1 |
  Where-Object { Test-Path (Join-Path $_.DirectoryName *.mp4) } |
  ForEach-Object {
    Start-Process -WorkingDirectory $_.DirectoryName powershell.exe "-NoExit -File `"$($_.FullName)`""
  }
  • Inverting the logic ensures that you call a given directory's ffmpeg.ps1 file only once, whereas invoking it once for each *.mp4 file in that directory, which is what your own approach attempted, is probably undesired.

  • The -WorkingDirectory parameter of Start-Process is used to set the working directory to the respective ffmpeg.ps1's directory.

  • The -File parameter of powershell.exe, the Windows PowerShell CLI, is used to invoke each target script, which is the preferred way to invoke script files (without it , powershell.exe defaults to -Command, which behaves subtly differently)

    • See this answer for guidance on when to use -File vs. -Command.
  • -NoExit is additionally passed so as to keep the window of the newly launched process open after the script terminates, to allow you to examine the results.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Sounds like a great idea! I made a small modification since I'm testing it in PowerShell ISE, it looks like this: `Get-ChildItem -Path "$env:USERPROFILE\Desktop\Cut" -Recurse -Filter "ffmpeg.ps1" | Where-Object { Test-Path $_.DirectoryName -Filter *.mp4 } ForEach-Object { Start-Process powershell.exe "-File `"$($_.FullName)`"" }` The issue is that for some time the PowerShell windows flicker quickly and do not run the scripts. Any idea how to resolve this? – Tyrone Hirt Dec 17 '22 at 21:49
  • 1
    @TyroneHirt, please see my update: `-WorkingDirectory` is now used to set the working directory to the directory in which the `ffmpeg.ps1` is located, and `-NoExit` has been added to keep the new window open, so you can inspect the output. I've also switched to `powershell.exe`, but please see the general advice re using the ISE in the next comment. – mklement0 Dec 17 '22 at 22:02
  • 1
    As an aside: The PowerShell ISE is [no longer actively developed](https://docs.microsoft.com/en-us/powershell/scripting/components/ise/introducing-the-windows-powershell-ise#support) and [there are reasons not to use it](https://stackoverflow.com/a/57134096/45375) (bottom section), notably not being able to run PowerShell (Core) 6+. The actively developed, cross-platform editor that offers the best PowerShell development experience is [Visual Studio Code](https://code.visualstudio.com/) with its [PowerShell extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell). – mklement0 Dec 17 '22 at 22:02
  • This worked perfectly!! Thank you very much for your help and tips, I've been studying PowerShell on my own for some time and I didn't know that PowerShell ISE is out of use. I will install VS Code and continue my studies there. Thank you very much for your help, I was breaking my head a few hours ago trying to solve this question. – Tyrone Hirt Dec 17 '22 at 22:13
  • I did some more tests and noticed that the script is starting the `ffmpeg scripts` even though there are no `*.mp4` files in the folder. I thought about adding an If condition somewhere to prevent this from happening, but I don't know if that's the best way. What do you recommend? – Tyrone Hirt Dec 17 '22 at 22:39
  • 1
    @TyroneHirt, my bad: [`Test-Path`](https://learn.microsoft.com/powershell/module/microsoft.powershell.management/test-path)'s `-Filter` parameter apparently doesn't work the way I thought it does - please see my update. – mklement0 Dec 17 '22 at 22:47
  • Excellent! I tested it and now it works perfectly. Once again, thank you very much for your help and for detailing each function of the script, I learned a lot from this post. – Tyrone Hirt Dec 17 '22 at 22:51
  • @TyroneHirt . – mklement0 Dec 17 '22 at 22:51