liquorishe
liquorishe

Reputation: 67

PowerShell Get video duration and list all files recursively, export to csv

I want to get a csv listing of all the video files (MTS or MP4 format) in a folder and its subfolders. I need the name with path, file size, and video duration.

I had used this PowerShell script to successfully get a list of all the files from the main folder and subfolders. But this script does not get the video file metadata from which I can extract the duration.

Get-ChildItem  D:\'My Source Folder' -Recurse | where {!$_.PSIsContainer} | select-object FullName, LastWriteTime, Length | export-csv -notypeinformation -path C:\results_file.csv | % {$_.Replace('"','')}

I'm completely new to PowerShell scripting, so I'm unable to figure out how to get the videofile metadata.

Note: I'm aware select-object * gets all the attributes of the file, but unfortunately that too does not have the duration which is specific to video files only.

I also found this answer, but I need a listing of all files in all subfolders, exported to a csv.

Upvotes: 3

Views: 11292

Answers (1)

sahus
sahus

Reputation: 378

This code should help

$Directory = "D:\My Source Folder"
$Shell = New-Object -ComObject Shell.Application
Get-ChildItem -Path $Directory -Recurse -Force | ForEach {
    $Folder = $Shell.Namespace($_.DirectoryName)
    $File = $Folder.ParseName($_.Name)
    $Duration = $Folder.GetDetailsOf($File, 27)
    [PSCustomObject]@{
        Name = $_.Name
        Size = "$([int]($_.length / 1mb)) MB"
        Duration = $Duration
    }
} | Export-Csv -Path "./temp.csv" -NoTypeInformation

Upvotes: 14

Related Questions