Search code examples
powershellmediainfo

Powershell MediaInfo read files in dir


I'm somewhat of a Powershell newbie, I've been using PS purposefully as my main scripting language for about 6 months now.

I'm trying to generate a basic script using only powershell and mediainfo to parse details from files in a directory (input) to another directory (output) of individual .txt files (Ideally with original file extensions removed).

The source and output filename should be the same, eg: test.mp4 -> test.txt. Here is what I've managed so far:

$indir = "C:\Temp\input"
$outdir = "C:\Temp\output\"
$mediainfo = "C:\Temp\MediaInfo_CLI_x64\MediaInfo.exe "

$files = Get-ChildItem $indir

foreach ($file in $files )
    {
    Write-Host "Scanning " $file.FullName
    Start-Process $mediainfo $file.FullName -NoNewWindow | out-file $outdir $file.Name
    }

I'm running into a lot of issues, and I feel like the solution is quite simple.

Thanks for any assistance.


Solution

  • Start-Process has parameters for redirecting STDIN, STDOUT and STDERR, so try this:

    Start-Process $mediainfo $file.FullName -NoNewWindow -Wait `
        -RedirectStandardOutput "$outdir\$($file.Name).txt"
    

    Normally you'd launch external programs with the call operator (&), though, which should let you use the regular output streams:

    & $mediainfo $file.FullName | Out-File "$outdir\$($file.Name).txt"
    

    If the program doesn't write to the success output stream (STDOUT) you can redirect the other streams like this (see about_Redirection for details):

    & $mediainfo $file.FullName *>&1 | Out-File "$outdir\$($file.Name).txt"