SquigglyMonkey
asked on
Script to find the latest changed file in a set of folders
Powershell one-liner or script that can find the latest changed file recursively in a folder, per folder 1 deep.
That might not make sense, I have shared folder "Shared", in that folder are 200 more folders, many of which may not have been used for a very long time. I'd like a list of the latest changed file (anywhere in that folder, or its own subfolders) in each of the 200 folders with the date. I hope that makes sense.
That might not make sense, I have shared folder "Shared", in that folder are 200 more folders, many of which may not have been used for a very long time. I'd like a list of the latest changed file (anywhere in that folder, or its own subfolders) in each of the 200 folders with the date. I hope that makes sense.
One liner (a long one liner):
Get-ChildItem c:\scripts -Dir |% { $topLev = $_; $file = $null; $delta = $null; Get-ChildItem $topLev.FullName -Recurse -File |% { if( $delta -lt $_.LastWriteTime ) { $delta = $_.LastWriteTime; $file = $_; } }; $file; }
Change c:\scripts to your interesting folder.
There is a space missing in oBdA's code, the first line should be
Get-ChildItem D:\Shared -Directory -Recurse | ForEach-Object {
Thanks, good eyes, Qlemo.
Added some eye candy to make the waiting more interesting.
Added some eye candy to make the waiting more interesting.
Get-ChildItem D:\Shared -Directory -Recurse | ForEach-Object {
Write-Host "Processing '$_.FullName'"
Get-ChildItem $_.FullName -File |
Sort-Object LastWriteTime -Descending |
Select-Object FullName, Name, DirectoryName, LastWriteTime -First 1
} | Export-Csv -NoTypeInformation -Path C:\Temp\LatestFiles.csv
ASKER
It did not want to run yesterday, just sat there and spun. I thought -directory was just a placeholder for my directory,
I understand it is supposed to be there, and now the script fails on it.
In my sea of 2016 servers, this one is 08 r2 still running powershell v2.
I see powershell 5.1 is able to run this script, as I tried it on another 08R2 server.
I'll update it a bit for what I need, like I removed full name and file name, I just need the folder and the date of last use.
Thanks!
I understand it is supposed to be there, and now the script fails on it.
In my sea of 2016 servers, this one is 08 r2 still running powershell v2.
I see powershell 5.1 is able to run this script, as I tried it on another 08R2 server.
I'll update it a bit for what I need, like I removed full name and file name, I just need the folder and the date of last use.
Thanks!
It's possible under PS 2.0 as well, will just take a bit longer because the type filtering happens at the pipeline instead of at the provider.
Get-ChildItem D:\Shared -Recurse | Where-Object {$_.PSIsContainer} | ForEach-Object {
Write-Host "Processing '$($_.FullName)'"
Get-ChildItem $_.FullName | Where-Object {-not $_.PSIsContainer} |
Sort-Object LastWriteTime -Descending |
Select-Object FullName, Name, DirectoryName, LastWriteTime -First 1
} | Export-Csv -NoTypeInformation -Path C:\Temp\LatestFiles.csv
ASKER
Thanks Fellas!
ASKER
The output looks exactly like I would have expected - one line per subfolder. Do you want only entries for the subfolders one level deep, but in each of them the most recent file date of all files in all subfolders? E.g.
D:\Shares\webpage some date
D:\Shares\application some date
D:\Shares\webpage some date
D:\Shares\application some date
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes! That did it, now I can find the folders that have not been used in years, some over 10. I can archive them and delete after some period of time.
Thank you again.
Thank you again.
Open in new window