简体   繁体   中英

Get the latest file from a bunch of similar named files in a directory - powershell

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

语法稍微简单一些:

$latest = Get-ChildItem C:\my_folder\my_file_????d.txt | Sort-Object LastAccessTime -Descending | Select -First 1

Nevermind, Figured it out myself from an example here

$dir = "c:\my_folder\"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Where-object { $_.Name -match "my_file_[0-9][0-9][0-9][0-9]d.txt"} | Select-Object -First 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM