Reputation: 24233
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.
Get-ChildItem | Sort lastwritetime -Descending
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
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