[英]How do you code in Powershell to get the latest date from an array?
I have folders as follow: - 10-17-17 - 10-18-17 - 10-19-17 - 10-20-17 我的文件夹如下:-10-17-17-10-18-17-10-19-17-10-20-17
I wanted to have PS to spit out that 10-20-17 is the latest folder located in my folder structure. 我想让PS吐出10-20-17是我文件夹结构中的最新文件夹。 I have no clue to go from here:
我没有线索可以从这里走:
$path = "C:\Users\JJames\Desktop\Folder Structure Test"
$todaydate = Get-Date -Format "MM-dd-yy"
$arr = Get-ChildItem $path |
Where-Object {$_.PSIsContainer} |
Foreach-Object {$_.Name}
I was able to get it spit out the names of each folder. 我能够吐出每个文件夹的名称。
You should be able to sort the object: 您应该能够对对象进行排序:
$path = "C:\Users\JJames\Desktop\Folder Structure Test"
$todaydate = Get-Date -Format "MM-dd-yy"
$folders = Get-ChildItem $path |
Where-Object {$_.PSIsContainer}
$folders | Sort-Object { [DateTime]::Parse($_.Name)} -Descending
That's the short way, without error handling for date formats. 这是简短的方法,没有错误处理日期格式。
To get only the first item (sticking with super quick and easy) 仅获得第一项(坚持超级快捷简便)
($dateStrings | Sort{ [DateTime]::Parse($_.Name)} -Descending)[0]
try this: 尝试这个:
$path = "C:\temp"
$dirDate=New-Object DateTime
Get-ChildItem $path -Directory | %{
if ([DateTime]::TryParseExact($_.Name,
"MM-dd-yy",
[System.Globalization.CultureInfo]::InvariantCulture,
[System.Globalization.DateTimeStyles]::None,
[ref] $dirDate)
)
{
$_ | Add-Member -MemberType NoteProperty "DateName" -Value $dirDate
$_
}
} | sort DateName -Descending | select -First 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.