Get data from last line and merge into data stream before removing

  • This script reads thru a file and removes the last line, but before it does that can I grab a value from that last row and add it

    into my output stream?  The value in the last line as an eye-catcher of $X and then grab the values from the 3rd position until

    it hits the next delimiter(,).



    $myFiles = Import-Csv -Delimiter '~' -Path $path -Header Path, ShortName
    # Then you can call it like so
    $myFiles.Path #Returns just the path
    $myFiles.ShortName # Returns the shortname
    $count = 0
    foreach ($myFile in $myFiles)
    get-childitem -path $myFile.Path *.spx | % {
    " Starting File Process....." >> $myFile.Pathsistem.log
    $PDate = get-date
    $date = get-date -uformat "_%d_%m_%Y_%H%M%S.bak"
    #"Set backup date to $date" >> $path\sistem.log
    $newname=($_.fullname -replace ".SPX",$date)
    $file = [System.IO.Path]::GetFileNameWithoutExtension($_.fullname)
    "File backup name is $newname" >> $myFile.Pathsistem.log
    Rename-Item $_.fullname $newname
    "Renamed file to $newname" >> $myFile.Pathsistem.log
    $lines = get-content $newname

    $count = 0

    foreach ($line in $lines[0..($lines.length -2)])

    $line + " " + $myFile.ShortName | Out-File "$path\sistem.csv" -append
    $count ++

    Write-Host $_.fullname
    "Files Processed on $PDate are $newname and Lines Processed $count " >> $path\sistem.log
  • Can you give a short example of what's in the file and what you want? Maybe a 2-3 line file?

  • Each file has what I'll call a total line(Last line to me).





    $Z,$I RW6 ,$N 1 ,$G201120,$H141340,$M00000000,$F000000,$X0000001240,$U0000

    I want the value from the $X(lastline of file) only, appended to my output file as the last field.

    This 0000001240 needs to be my last field in output file.


  • The $Z line will always be the last line in the file and need to get the value from $X into my output before cleanup of that line.


Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply