I have a script that I used very frequently to get a list of files and then process them with ffmpeg, the beginning of which read:
$files = Get-ChildItem -Path "Season *\*" -Include *.mp4, *.mkv, *.m4v, *.avi, *.wmv, *.mpg -recurse
ForEach ($file in $files) {
$parent = Split-Path $file -parent
$origFile = $parent+'\'+$file.name
$shortName = $file.name
....
I needed a solution to be able to use it in directories that was more flexible, but avoided the junctions in a directory (the recurse command will travel forever down the junctions). I found an answer here on superuser, link. I wrapped my original script in a function and modified the 4th line of the recurse function found on here to call the function and process the files as they were found.
function Recurse($path) {
$fc = new-object -com scripting.filesystemobject
$folder = $fc.getfolder($path)
foreach ($i in $folder.files) {if ($i.name -match '.*\.(mp4|mkv|m4v|avi|wmv|mpg)$') {processFile($i)}}
foreach ($i in $folder.subfolders) {
$i | select Path
if ( (get-item $i.path).Attributes.ToString().Contains("ReparsePoint") -eq $false) {
Recurse($i.path)
}
}
}
function processFile($file) {
$parent = Split-Path $file -parent
$origFile = $parent+'\'+$file.name
$shortName = $file.name
...
But, clearly the way the recurse function operates and generates the list does not create the same type of file-object(?) that the get-childitem does. The operations like "Split-Path $file -parent" now do not work and instead return nothing. Everything I've tested system to indicate that the variable $file is a System._ComObject, which I'm assuming is set in that first command of the recurse functions. Is that not what Get-Childitem returns? How do I get the two functions to work together?
I'm open to modifying either the original script, or the recurse function so that the original capability (such as calling Split-Path) works again.