2

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.

1 Answer 1

1

System._ComObject, which I'm assuming is set in that first command of the recurse functions. Is that not what Get-Childitem returns?

No. The COM objects returned from the scripting.filesystemobject are different types of objects then what you get from the Powershell File system provider. The Powershell provider basically is composed of .NET System.IO.FileInfo objects. While it may seem like they are the same, since they are both dealing with filesystem operations, the two types of objects are not compatible.

Microsoft does provide a document that suggest conversions.

In fact your script already uses a method to convert them, in one of your if conditionals. The bit where you do a get-item $i.path. That code fragment basically converting one type of object to the other. It has Get-Item return a powershell filesystem object, and to do that, you give the Get-Item command the full path of the returned by the scripting.filesystemobject value you have in $i. You should be able to do this same trick in your other places as well.

1
  • Thank you! I had found the get-item command, but I was trying everything except for $i.path to get it to work. I tried strings, $i.name, $i, but not $i.path. That worked perfectly! I didn't write the recurse function, so I wasn't familiar with it's inner workings :)
    – J M
    Feb 27, 2016 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.