我需要从下面的列表中获取最新的文件。每天创建3种类型的文件,我只需要3种类型的文件。
示例:
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
我有来自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
答案 0 :(得分:2)
语法稍微简单:
$latest = Get-ChildItem C:\my_folder\my_file_????d.txt | Sort-Object LastAccessTime -Descending | Select -First 1
答案 1 :(得分:0)
没关系,从示例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