-1

I have many folders with even more subfolders, and as posted in my first question

How to create a powershell script / or windows .bat file for ffmpeg

I want to encode all video files in the folders. The Script I got from mklement0 works fine but lazy as I am, I was wondering if there was a way to tell the PowerShell to enter folder 1, go to subfolder_1, and execute the ps1 script (would be perfect if it executed in a new powershell instance), wait a certain time and go into subfolder_2

Repeat until no more subfolders available.

Is this possible?

Edit: The Script I got:

Get-ChildItem *.mkv | where BaseName -notlike '*`[encoded]' | foreach {
ffmpeg -i $_ -c:v libx265 -c:a copy -x265-params crf=25 "$($_.BaseName)[encoded].mkv"
pause
}

What is the reason for the desire to process each subfolder in a separate instance of powershell.exe? by Mathias R. Jessen

Because I want to encode multiple folders at once to save some time. If there is a way to execute the script in the same PowerShell (as far as my understanding goes, I can only encode one folder at one time if I use the same PowerShell instance)

WorldTeacher
  • 93
  • 1
  • 10
  • Please include all details pertinent to the question _in the post_ (even when linking to another SO post). What is the reason for the desire to process each subfolder in a separate instance of powershell.exe? – Mathias R. Jessen Oct 05 '20 at 17:30
  • You can use jobs to do all at once. They are basically mini powershells that open and do the requested work in the background. use `start-job {encoding commands here}` - be careful with this as you can bog your system down REALLY fast. You may want to do something in your loop like `$i += 1` to iterate through numbers followed by `if ($i % 5 -eq 0) {get-job | wait-job}` this will allow 5 jobs to open, and every 5 it will wait till all 5 are done. It's not the most efficient method of parallel CPU usage, but it's better than killing your system. – Robert Cotterman Oct 06 '20 at 04:37

1 Answers1

0

You could wrap the whole thing in another Get-ChildItem to find all the subdirectories in your main folder, then set-location to that path and run in each of those:

$directories = get-childitem -path "C:\Path\To\Videos\Folder" -Recurse -Directory
Foreach ($dir in $directories) {
    Set-Location $dir.fullname
    *Run your code here*
}

This would be better than trying to do them all in parallel with different PowerShell instances. Correct me if I'm wrong, but video encoding uses a lot of your computer's resources, so trying to run them in parallel would actually be more of a hindrance.

Bod
  • 61
  • 3