如何使用 PowerShell 脚本在目录中查找最新文件? [英] How do I find the newest file in a directory using a PowerShell script?
本文介绍了如何使用 PowerShell 脚本在目录中查找最新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,如果我有一个包含以下文件的目录:
If, for example, I have a directory which contains the following files:
Test-20120626-1023.txt
Test-20120626-0710.txt
Test-20120626-2202.txt
Test-20120626-1915.txt
Test-20120626-1142.txt
如您所见,每个文件名都包含创建时间,并采用可排序的格式.
As you can see, each file name contains the time of creation which is in a sortable format.
如何找到最新的文件名(在本例中为 Test-20120626-2202.txt
)并将其存储在变量中?
How do I find the name of the latest file name (in this case Test-20120626-2202.txt
) and store it in variable?
注意:如果目录路径有任何不同,它也会存储在一个变量中.
推荐答案
你可以试试这样的:
$dir = "C:\test_code"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.name
这篇关于如何使用 PowerShell 脚本在目录中查找最新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文