oymonk
oymonk

Reputation: 365

How create CSV containing both file metadata and file content?

Using Scripting Guy's code, I can create a csv that shows the metadata associated to the files on my computer:

enter image description here

I would like to add an additional column to this csv that contains the entire content of the file. I found the Get-Content cmd and was able to get it to work, but I cannot figure out how to insert it into the code.

I've tried a variety of approaches:

if($objFolder.getDetailsOf($File, $a))
           {
             $hash += @{$($objFolder.getDetailsOf($objFolder.items, $a))  =
                   $($objFolder.getDetailsOf($File, $a))
                   & $($objFolder.get-content($File, $a)) }
            $FileMetaData | Add-Member $hash
            $hash.clear() 
           } #end if

Error code: The hash literal was incomplete.

if($objFolder.getDetailsOf($File, $a))
           {
             $hash += @{$($objFolder.getDetailsOf($objFolder.items, $a))  =
                   $($objFolder.getDetailsOf($File, $a)) }
            $FileMetaData | Add-Member $hash
            $FileMetData | Get-Content($objFolder)
            $hash.clear() 
           } #end if

Error code: a positional parameter cannot be found that accepts the argument 'recurse'

edit: here is the full code:

  Function Get-FileMetaData 
{
Param([string[]]$folder)
 foreach($sFolder in $folder)
  {
   $a = 0
   $objShell = New-Object -ComObject Shell.Application
   $objFolder = $objShell.namespace($sFolder)

   foreach ($File in $objFolder.items())
    { 
     $FileMetaData = New-Object PSOBJECT
      for ($a ; $a  -le 266; $a++)
       { 
  if($objFolder.getDetailsOf($File, $a))
               {
                 $hash += @{$($objFolder.getDetailsOf($objFolder.items, $a))  =
                       $($objFolder.getDetailsOf($File, $a))}
                $FileMetaData | Add-Member $hash
                $hash.clear() 
               } #end if
       } #end for 
     $a=0
     $FileMetaData
    } #end foreach $file
  } #end foreach $sfolder
} #end Get-FileMetaData
$picMetadata  = Get-FileMetaData -folder (Get-childitem C:\Olivia\ -Recurse -Directory).FullName


$picMetadata |

Select Name,'Date created','Folder name'|

Export-CSV -Path C:\Olivia\metadata.csv -NoTypeInformation

Upvotes: 0

Views: 288

Answers (1)

ArcSet
ArcSet

Reputation: 6860

OK so the issue was where to put the Get-content

You have 3 loops happening inside each other.

  1. The most outward is For each folder inside the array of folders from the parameters. This one creates a shell com object and get the information about the folder.

  2. The next one loops through every item inside the folder. And creates a PSObject $FileMetaData

  3. The last one and most inner gets the item details looping through 266 options. Then adds them to the PSObject.

Loop 2 is where we need to add a new property to the PS object and add the content so it only get looped once per Item. We also need to make sure its not a folder as a folder has no file contents.

This was the change added to the function in the 2nd loop

$FileMetaData | Add-Member -MemberType NoteProperty -Name Content -Value $(
    If($File.IsFolder -eq $false){
        Get-Content $File.Path
    }else{
        $null
    }
)

here is the remake of the function

Function Get-FileMetaData{
Param([string[]]$folder)
foreach($sFolder in $folder){
    $a = 0
    $objShell = New-Object -ComObject Shell.Application
    $objFolder = $objShell.namespace($sFolder)
    foreach ($File in $objFolder.items()){
        $FileMetaData = New-Object PSOBJECT
        for ($a ; $a  -le 266; $a++)
        { 
            if($objFolder.getDetailsOf($File, $a))
            {
            $hash += @{$($objFolder.getDetailsOf($objFolder.items, $a))  =
                    $($objFolder.getDetailsOf($File, $a)) }
            $FileMetaData | Add-Member $hash
            $hash.clear() 
            }
        }
        $a=0
        $FileMetaData | Add-Member -MemberType NoteProperty -Name Content -Value $(
            If($File.IsFolder -eq $false){
                Get-Content $File.Path
            }else{
                $null
            }
        )
         $FileMetaData
        }
    }
}

Upvotes: 1

Related Questions