Home > Net >  Is there a way to display the latest file of multiple paths with information in a table format?
Is there a way to display the latest file of multiple paths with information in a table format?

Time:09-23

I check every day, whether a CSV-File has been exported to a specific folder (path). At the moment there are 14 different paths with 14 different files to check. The files are being stored in the folder and are not deleted. So i have to differ between a lot of files with "lastwritetime". I would like a code to display the results in table format. I would be happy with something like this:

Name LastWriteTime Length

ExportCSV1 21.09.2022 00:50 185

ExportCSV2 21.09.2022 00:51 155

My code looks like this:

$Paths = @('Path1', 'Path2', 'Path3', 'Path4', 'Path5', 'Path6', 'Path7', 'Path8', 'Path9', 'Path10', 'Path11', 'Path12', 'Path13', 'Path13')

foreach ($Path in $Paths){
    Get-ChildItem $path | Where-Object {$_.LastWriteTime}|
select -last 1

Write-host $Path
}

pause

This way i want to make sure, that the files are being sent each day. I get the results that i want, but it is not easy to look at the results individually. I am new to powershell and would very much appreciate your help. Thank you in advance.

CodePudding user response:

Continuing from my comments, here is how you could do this:

$Paths = @('Path1', 'Path2', 'Path3', 'Path4', 'Path5', 'Path6', 'Path7', 'Path8', 'Path9', 'Path10', 'Path11', 'Path12', 'Path13', 'Path13')

$Paths | ForEach-Object {
    Get-ChildItem $_ | Where-Object {$_.LastWriteTime} | Select-Object -Last 1
} | Format-Table -Property Name, LastWriteTime, Length

If you want to keep using foreach() instead, you have to wrap it in a scriptblock {…} to be able to chain everything to Format-Table:

. {
    foreach ($Path in $Paths){
        Get-ChildItem $path | Where-Object {$_.LastWriteTime} | Select-Object -Last 1
    }
} | Format-Table -Property Name, LastWriteTime, Length

Here the . operator is used to run the scriptblock immediately, without creating a new scope. If you want to create a new scope (e. g. to define temporary variables that exist only within the scriptblock), you could use the call operator & instead.

  • Related