Advertisement
upz

Get-MetaData

upz
Apr 15th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-MetaData
  2. {
  3. PARAM
  4. (
  5.         [PARAMETER(Mandatory=$true)]
  6.         [string]$path = ""
  7.     ,   [PARAMETER( Mandatory=$true,
  8.                     HelpMessage="extension IE. .mp3, .txt or .mov")]
  9.         [string]$type = ""#'mp3'
  10.     ,   [switch]$recurse
  11. )
  12.     if ($recurse)
  13.     {
  14.         $LPath = Get-ChildItem -Path $path -Directory -Recurse
  15.     } else {$LPath = $path}
  16.  
  17.     $DirectoryCount = 1
  18.     $RetrievedMetadata = $true
  19.     $OutputList = New-Object 'System.Collections.generic.List[psobject]'
  20.  
  21.     Foreach ($pa in $LPath)
  22.     {
  23.         $shell = New-Object -ComObject shell.application
  24.  
  25.         if ($recurse)
  26.         {
  27.             $objshell = $shell.NameSpace($pa.FullName)
  28.         } else {$objshell = $shell.NameSpace($pa)}
  29.         #Build data list
  30.         $count = 0
  31.  
  32.         #Filter on filetype
  33.         $filter = $objshell.items() | where {$_.path -match $type}
  34.         foreach ($file in $filter)
  35.         {
  36.             if ($RetrievedMetadata)
  37.             {
  38.                 # Build metanumbers
  39.                 Write-Verbose "Building MetaIndex for filetype $type"
  40.                 $Metanumbers = New-Object -TypeName 'System.Collections.Generic.List[int]'
  41.                 for ($a = 0 ; $a -le 400;$a++)
  42.                 {
  43.                     if ($objshell.GetDetailsOf($file,$a))
  44.                     {
  45.                         $Metanumbers.Add([int]$a)
  46.                     }    
  47.                 }
  48.                 $RetrievedMetadata = $false
  49.                 Write-Verbose "$($Metanumbers.Count) entries in MetaIndex for filetype $type"    
  50.             }
  51.  
  52.             $count++
  53.             $CurrentDirectory = Get-ChildItem -Path $file.path
  54.             try{
  55.             Write-Progress -Activity " Getting metadata from $DirectoryCount/$($LPath.count) $($CurrentDirectory[0].DirectoryName)" -Status "Working on $count/$($filter.count) - $($file.Name)" -PercentComplete (($count / $filter.count) * 100) -ErrorAction stop
  56.             }catch{}
  57.  
  58.             #Build Hashtable for each file
  59.             $Hash = [ordered]@{}
  60.             foreach ($nr in $Metanumbers)
  61.             {
  62.                 $PropertyName = $($objshell.GetDetailsOf($objshell.Items, $nr))
  63.                 $PropertyValue = $($objshell.GetDetailsOf($File, $nr))
  64.                 $Hash[$PropertyName] = $PropertyValue
  65.                 $Hash.Remove("")
  66.             }
  67.                        
  68.             $FileMetaData = New-Object -TypeName PSobject -Property $hash
  69.             $OutputList.Add($FileMetaData)
  70.             $hash = $null
  71.         }
  72.         $DirectoryCount++        
  73.     }
  74.     Write-Verbose "MetaData for $($OutputList.count) files found"
  75.     return $OutputList    
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement