Link to home
Start Free TrialLog in
Avatar of SquigglyMonkey
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.
Avatar of oBdA
oBdA

Try that; if you feel like it, you can squeeze it into a single line:
Get-ChildItem D:\Shared-Directory -Recurse | ForEach-Object {
	Get-ChildItem $_.FullName -File |
	Sort-Object LastWriteTime -Descending |
	Select-Object FullName, Name, DirectoryName, LastWriteTime -First 1
} | Export-Csv -NoTypeInformation -Path C:\Temp\LatestFiles.csv

Open in new window

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; }

Open in new window

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 {

Open in new window

Thanks, good eyes, Qlemo.
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 

Open in new window

Avatar of SquigglyMonkey

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!
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  

Open in new window

Thanks Fellas!
Well shoot, I take that back, looking at the output a little more carefully, I see that it is putting out more than one file per folder.
User generated image
I just need 1 per folder recursively,,,
For instance, from the picture above  I just want,
D:\shares\webpage       5/2/2017 15:58
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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.