powershellscriptingmediainfo

Powershell with mediainfo command for each file in directory


I'm trying to execute the command: .\mediainfo.exe --Output=XML "$path\file\*.*" > .\file\$output.xml for multiple media files in a folder. Here's what I have currently:

$path = get-item F:\Desktop\work\
cd $path
$output = (Get-ChildItem '.\file\*.*').Basename
.\mediainfo.exe --Output=XML "$path\file\*.*" > .\file\$output.xml

My issue is that MediaInfo.exe will create on XML output file containing all information from all media in the folder F:\Desktop\work\file.

I tried the following:

$path = get-item F:\Desktop\work\
$dir = get-item $path\file | ? {$_.PSIsContainer}
cd $path
$output = (Get-Item '.\file\*.*').Basename
ForEach ($d in $dir){.\mediainfo.exe --Output=XML "$d" > .\file\$output.xml
}

But same thing. Name of the output.xml will be concatenated with all media name. For instance if I have two files name audio1.aif and audio2.aif, the name of my XML will be audio1 audio2.xml

Any idea how to achieve that ? Thanks


Solution

  • Maybe I missed something in the question, but for having a sidecar XML file with its file name being the source file name + ".xml", this small script does it:

    ForEach ($d in get-item "file\*.*"){
      .\mediainfo.exe --Output=XML "$d" > "$d.xml"
    }
    

    Note: the resulting MediaInfo XML will have the absolute file name (including the path) because the absolute file name is provided to MediaInfo. If you prefer a relative file name (without the current path, and without the directory name containing the files), you need to provide relative file names:

    $current = (Get-Item .).FullName
    cd file
    ForEach ($d in Get-Item "*.*"){
      $filename = Split-Path "$d" -Leaf
      ..\mediainfo.exe --Output=XML $filename > "$d.xml"
    }
    cd $current
    

    If you want the XML file without the source file extension (not recommended, issue if you get 2 files with same base name e.g. a.avi and a.srt a sidecar subtitle file for the first file), you need an intermediate variable:

    $current = (Get-Item .).FullName
    cd file
    ForEach ($d in Get-Item "*.*"){
      $filename = Split-Path "$d" -Leaf
      #$basename = Split-Path "$d" -LeafBase #Only in newest versions
      $basename = (Get-Item -Path "$d").BaseName
      ..\mediainfo.exe --Output=XML $filename > "$basename.xml"
    }
    cd $current