Sylveons
Sylveons

Reputation: 13

How do I write a Powershell script that checks when the last time a file was added to a folder?

I'm currently writing a script that checks each folder in a directory for the last time a file was written to each folder. I'm having trouble figuring out how to obtain the last time a file was written to the folder, as opposed to just retrieving the folder's creation date.

I've tried using Poweshell's recursive method, but couldn't figure out how to properly set it up. Right now, the script successfully prints the name of each folder to the Excel spreadsheet, and also print the last write time of each folder, which is the incorrect information.

$row = 2

$column = 1

Get-ChildItem "C:\Users\Sylveon\Desktop\Test"| ForEach-Object {     

    #FolderName
    $sheet.Cells.Item($row,$column) = $_.Name
    $column++
    #LastBackup
    $sheet.Cells.Item($row,$column) = $_.LastWriteTime
    $column++
    #Increment to next Row and reset Column
    $row++
    $column = 1
}

The current state of the script prints each folder name to the report, but gives the folders creation date rather than the last time a file was written to that folder.

Upvotes: 1

Views: 344

Answers (2)

dodecahedragon
dodecahedragon

Reputation: 16

If you're only looking at the first level of files in each folder, you can do it using a nested loop:

$row = 2
$column = 1

$folders = Get-ChildItem $directorypath
ForEach ($folder in $folders) { 

    # start off with LastEdited set to the last write time of the folder itself
    $LastEdited = $folder.LastWriteTime
    $folderPath = $directoryPath + '\' + $folder.Name
    # this 'dynamically' sets each folder's path

    $files = Get-Childitem $folderPath
    ForEach ($file in $files) {
        if ((Get-Date $file.LastWriteTime) -gt (Get-Date $LastEdited)) {
            $LastEdited = $file.LastWriteTime
        }
    }

    $sheet.Cells.Item($row,$column) = $folder.Name
    $column++
    $sheet.Cells.Item($row,$column) = $LastEdited
    $row++
    $column = 1
}

Upvotes: 0

Elizabeth
Elizabeth

Reputation: 499

The following should work to get the most recent edit date of any file in the current directory.

Get-ChildItem | Sort-Object -Property LastWriteTime -Descending | Select-Object -first 1 -ExpandProperty "LastWriteTime"

Get-ChildItem gets items in your directory

Sort-Object -Property LastWriteTime -Descending sorts by write-time, latest first

Select-Object -first 1 -ExpandProperty "LastWriteTime" gets the first one in the list, then gets its write-time


I made this to get the data you're trying to get. The last line gives us an empty string if the directory is empty, which is probably what's safest for Excel, but you could also default to something other than an empty string, like the directory's creation date:

$ChildDirs = Get-ChildItem | Where-Object { $_ -is [System.IO.DirectoryInfo] }
$EditNames = $ChildDirs | ForEach-Object Name
$EditTimes = $EditNames | ForEach-Object { @( (Get-ChildItem $_ | Sort-Object -Property LastWriteTime -Descending | Select-Object -first 1 LastWriteTime), '' -ne $null)[0] }

for($i=0; $i -lt $ChildDirs.Length; $i++) {
    Write-Output $EditNames[$i]
    Write-Output $EditTimes[$i]
}


To implement this for what you're doing, if I understand your question correctly, try the following:

$ChildDirs = Get-ChildItem | Where-Object { $_ -is [System.IO.DirectoryInfo] }
$EditNames = $ChildDirs | ForEach-Object Name
$EditTimes = $EditNames | ForEach-Object { @( (Get-ChildItem $_ | Sort-Object -Property LastWriteTime -Descending | Select-Object -first 1 LastWriteTime), '' -ne $null)[0] }

for($i=0; $i -lt $ChildDirs.Length; $i++) {
    #FolderName
    $sheet.Cells.Item($row, $column) = $EditNames[$i]
    $column++
    #LastBackup
    $sheet.Cells.Item($row, $column) = $EditTimes[$i]
    $row++
    $column = 1
}

Upvotes: 2

Related Questions