Home > Enterprise >  Appending file name and last write time as columns in a CSV
Appending file name and last write time as columns in a CSV

Time:10-28

I have bunch of text files that I am converting to a CSV. For example I have a few hundred txt files that look like this

Serial Number : 123456
Measurement : 5
Test Data : 125

And each file is being converted to a single row on the CSV. I can't figured out how to add an additional column for the file name and the last write time.

This is what I currently have that copies all of the data from txt to CSV

$files = "path"
function Get-Data {
    param (
        [Parameter (Mandatory, ValueFromPipeline, Position=0)] $filename
    )


    $data=@{}
    $lines=Get-Content -LiteralPath $filename | Where-Object {$_ -notlike '*---*'}
    foreach ($line in $lines) {
            $splitLine=$line.split(":")
            $data.Add($splitLine[0],$splitLine[1])
            
             
       
    }
    return [PSCustomObject]$data
    
}
$files | Foreach-Object -Process {Get-Data $_} | Export-Csv -Path C:\Scripts\data.csv -NoTypeInformation -Force

I've tried doing this but it doesn't add anything. I might be trying to add the data the wrong way.

$files = "path"
function Get-Data {
    param (
        [Parameter (Mandatory, ValueFromPipeline, Position=0)] $filename
    )


    $data=@{}
    $name = Get-ChildItem -literalpath $filename | Select Name
    $data.Add("Filename", $name)
    $lines=Get-Content -LiteralPath $filename | Where-Object {$_ -notlike '*---*'}
    foreach ($line in $lines) {
            $splitLine=$line.split(":")
            $data.Add($splitLine[0],$splitLine[1])
            
             
       
    }
    return [PSCustomObject]$data
    
}
$files | Foreach-Object -Process {Get-Data $_} | Export-Csv -Path E:\Scripts\Pico2.csv -NoTypeInformation -Force

CodePudding user response:

Here's a streamlined version of your code that should work as intended:

function Get-Data {
  param (
    [Parameter (Mandatory, ValueFromPipeline)] 
    [System.IO.FileInfo] $file # Accept direct output from Get-ChildItem 
  )

  process { # Process each pipeline input object

    # Initialize an ordered hashtable with 
    # the input file's name and its last write time.
    $data = [ordered] @{
      FileName = $file.Name
      LastWriteTime = $file.LastWriteTime
    }

    # Read the file and parse its lines
    # into property name-value pairs to add to the hashtable.
    $lines = (Get-Content -ReadCount 0 -LiteralPath $file.FullName) -notlike '*---*'
    foreach ($line in $lines) {
      $name, $value = ($line -split ':', 2).Trim()
      $data.Add($name, $value)
    }

    # Convert the hashtable to a [pscustomobject] instance
    # and output it.
    [PSCustomObject] $data

  }

}

# Determine the input files via Get-ChildItem and
# pipe them directly to Get-Data, which in turn pipes to Export-Csv.
Get-ChildItem "path" | 
  Get-Data |
    Export-Csv -Path C:\Scripts\data.csv -NoTypeInformation -Force
  • Related