Reputation: 11090
I need to get the latest file from the below list.There are 3 types of files created everyday and I just need 1 from the 3 types.
Example:
my_file_dob_6545d.txt 12/3/2017 5:00 PM
my_file_csm_6545d.txt 12/3/2017 5:00 PM
my_file_6545d.txt 12/3/2017 5:00 PM <--- Need to get this.
my_file_dob_6544d.txt 12/2/2017 5:00 PM
my_file_csm_6544d.txt 12/2/2017 5:00 PM
my_file_6544d.txt 12/2/2017 5:00 PM
my_file_dob_6543d.txt 12/1/2017 5:00 PM
my_file_csm_6543d.txt 12/1/2017 5:00 PM
my_file_6543d.txt 12/1/2017 5:00 PM
I have the following script from here
$dir = "c:\my_folder\"
$filter="my_file_*d.txt" <--How can modify this to get only my_file_XXXXd.txt files? XXXX keeps incrementing everyday
$latest = Get-ChildItem -Path $dir -Filter $filter | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.name
Upvotes: 0
Views: 143
Reputation: 36342
Slightly simpler syntax:
$latest = Get-ChildItem C:\my_folder\my_file_????d.txt | Sort-Object LastAccessTime -Descending | Select -First 1
Upvotes: 2