DavDav
DavDav

Reputation: 127

Powershell: Create CSV list containing newest line in newest file in N number subdirs

I'm pretty green in terms of Powershell, so this presents a challenge to me:

Objective: Traverse N number of subdirs containing logs, find the newest file in each subdir, extract the last written line and add this to file.

This lists the directories containing logs - these dirs come and go beyond my control.

Get-ChildItem Z:\Logfiles | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-1)}

and I have found this snippet to extract the last line from last written file in a named dir, where each dir contains logs from server named as folder name (mostly these are IP addresses):

gci Z:\Logfiles\172.16.1.1 | sort LastWriteTime | select -last 1 | Get-Content | Select-Object -Last 1

I need to merge these so that I can append these last lines to a file, perhaps as CSV - all hints are appreciated.

Upvotes: 0

Views: 66

Answers (2)

Santiago Squarzon
Santiago Squarzon

Reputation: 59772

You could use something like this, I added comments so it's easier to understand the logic.

# List all folders on the Initial Path
$folders = Get-ChildItem -Directory -Path Z:\Logfiles
$export = [system.collections.generic.list[pscustomobject]]::new()

foreach($folder in $folders)
{
    # Get the newest file in each folder
    $file = Get-ChildItem $folder -File | Sort-Object LastWriteTime -Descending | Select -First 1

    # Read the content of the file and get the last line
    $content = (Get-Content $file)[-1]

    # Here you can create a new object that will be used to export to Csv
    # As an example, i'll create an object with the File Name,
    # Last Write Time and Last Line of the Content (This will be the CSV columns)
    $export.Add(
        [PSCustomObject]@{
            FileName = $file.Name
            LastWriteTime = $file.LastWriteTime
            LastLine = $content
    })
}

# After the loop finishes you can export the results to a Csv
$export | Export-Csv -NoTypeInformation -Path "Path/To/Export/Here.csv"

Upvotes: 1

Dave Sexton
Dave Sexton

Reputation: 11188

Use the -File and -Recurse flags of Get-Childitems, this will give you all the files in the root and sub folders. Pipe this into Group-Object and group on directory. All you need to do then is sort the grouped files by date and select the first one. Something like this:

Get-ChildItem -File -Recurse | Group-Object Directory | % {
    New-Object psobject -Property @{Folder = $_.Name; File = ($_.Group | Sort-Object LastWriteTime -Descending)[0]}
} | Export-Csv your_file_name.csv

Upvotes: 0

Related Questions