Ron
Ron

Reputation: 24233

Sort a directory​ then find a file on lastwritetime in that dir

I have a directory with lots of sub-directories which in turn have files. The file I'm looking for mostly is in the latest modified sub-directory.

This setup is on UNC.

To find the file as quickly as possible I want to first sort the sub-directory on last modified time and then search.

  1. Get-ChildItem | Sort lastwritetime -Descending

  2. Get-ChildItem filename.txt -Recurse | Where-Object {$_.lastwritetime -gt "MM/dd/yyyy" -and $_.lastwritetime -lt "MM/dd/yyyy"}

I want to pass the output of 1 to 2.

Upvotes: 0

Views: 226

Answers (1)

colsw
colsw

Reputation: 3326

Get-ChildItem | Sort LastWriteTime | Select -Last 1

will get the most recent file, you can change -Last for -First to get the oldest unmodified file.

you can also specify -File or -Directory on Get-ChildItem to make sure you get only files/dirs.

edit: after your question update.

$SearchIn = Get-ChildItem -Directory | Sort LastWriteTime | Select -Last 1
Get-ChildItem $SearchIn.FullName -Recurse | Where-Object {$_.lastwritetime -gt "MM/dd/yyyy" -and $_.lastwritetime -lt "MM/dd/yyyy"}

Upvotes: 1

Related Questions