从文件名中提取时间戳并排序
Extract timestamp from filename and sort
我正在尝试查看文件夹中的每个项目,并将每个项目添加到按文件名中的日期戳排序的数组中。
比如我有三个文件:
- myfile_20150813_040949.txt
- myfile_20150812_030949.txt
- myfile_20150812_010949.txt
我不确定如何从每个时间中解析出时间并将它们按升序添加到数组中。任何帮助将不胜感激。
我假设您希望通过使用此示例从文件名中提取的已解析时间戳对文件进行排序。它可能不是最好的 RegEx 方法,但它在测试中有效。
#RegEx pattern to parse the timestamps
$Pattern = '.*_(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})\.txt'
$List = New-Object System.Collections.ArrayList
$Temp = New-Object System.Collections.ArrayList
Get-ChildItem | ForEach {
#Make sure the file matches the pattern
If ($_.Name -match $Pattern) {
Write-Verbose "Add $($_.Name)" -Verbose
$Date = $Matches[2],$Matches[3],$Matches[1] -join '/'
$Time = $Matches[4..6] -join ':'
[void]$Temp.Add(
(New-Object PSObject -Property @{
Date =[datetime]"$($Date) $($Time)"
File = $_
}
))
}
}
#Sort the files by the parsed timestamp and add to the main list
$List.AddRange(@($Temp | Sort Date | Select -Expand File))
#Clear out the temp collection
$Temp.Clear()
#Display the results
$List
您可以为此使用字符串方法 .Split()
和 TryParseExact()
的 [datetime]
方法。遍历每个文件并为 "FromFileDate" 添加一个 属性,然后对其进行排序。
$path = "C:\temp"
Get-ChildItem -Filter "*.txt" -Path $path | ForEach-Object{
$date = ($_.BaseName).Split("_",2)[1]
$result = New-Object DateTime
if([datetime]::TryParseExact($date,"yyyyMMdd_hhmmss",[System.Globalization.CultureInfo]::InvariantCulture,[System.Globalization.DateTimeStyles]::None,[ref]$result)){
# This is a good date
Add-Member -InputObject $_ -MemberType NoteProperty -Name "FromFileDate" -Value $result -PassThru
} Else {
# Could not parse date from filename
Add-Member -InputObject $_ -MemberType NoteProperty -Name "FromFileDate" -Value "Could not Parse" -PassThru
}
} | Select-Object Name,fromfiledate | Sort-Object fromfiledate
我们采用每个文本文件的基本名称,并将其从第一个下划线开始分成两部分。然后,我们使用 TryParseExact
尝试将 "date" 字符串转换为 "yyyyMMdd_hhmmss" 的格式。由于我们使用 TryParseExact
如果我们在解析日期时遇到问题,那么代码将继续。
示例输出
Name FromFileDate
---- ------------
myfile_20150812_030949.txt 8/12/2015 3:09:49 AM
myfile_20150813_040949.txt 8/13/2015 4:09:49 AM
files.txt Could not Parse
如果您不希望输出中出现错误数据,一个简单的 Where-Object{$_.fromfiledate -is [datetime]}
将删除这些条目。
我正在尝试查看文件夹中的每个项目,并将每个项目添加到按文件名中的日期戳排序的数组中。
比如我有三个文件:
- myfile_20150813_040949.txt
- myfile_20150812_030949.txt
- myfile_20150812_010949.txt
我不确定如何从每个时间中解析出时间并将它们按升序添加到数组中。任何帮助将不胜感激。
我假设您希望通过使用此示例从文件名中提取的已解析时间戳对文件进行排序。它可能不是最好的 RegEx 方法,但它在测试中有效。
#RegEx pattern to parse the timestamps
$Pattern = '.*_(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})\.txt'
$List = New-Object System.Collections.ArrayList
$Temp = New-Object System.Collections.ArrayList
Get-ChildItem | ForEach {
#Make sure the file matches the pattern
If ($_.Name -match $Pattern) {
Write-Verbose "Add $($_.Name)" -Verbose
$Date = $Matches[2],$Matches[3],$Matches[1] -join '/'
$Time = $Matches[4..6] -join ':'
[void]$Temp.Add(
(New-Object PSObject -Property @{
Date =[datetime]"$($Date) $($Time)"
File = $_
}
))
}
}
#Sort the files by the parsed timestamp and add to the main list
$List.AddRange(@($Temp | Sort Date | Select -Expand File))
#Clear out the temp collection
$Temp.Clear()
#Display the results
$List
您可以为此使用字符串方法 .Split()
和 TryParseExact()
的 [datetime]
方法。遍历每个文件并为 "FromFileDate" 添加一个 属性,然后对其进行排序。
$path = "C:\temp"
Get-ChildItem -Filter "*.txt" -Path $path | ForEach-Object{
$date = ($_.BaseName).Split("_",2)[1]
$result = New-Object DateTime
if([datetime]::TryParseExact($date,"yyyyMMdd_hhmmss",[System.Globalization.CultureInfo]::InvariantCulture,[System.Globalization.DateTimeStyles]::None,[ref]$result)){
# This is a good date
Add-Member -InputObject $_ -MemberType NoteProperty -Name "FromFileDate" -Value $result -PassThru
} Else {
# Could not parse date from filename
Add-Member -InputObject $_ -MemberType NoteProperty -Name "FromFileDate" -Value "Could not Parse" -PassThru
}
} | Select-Object Name,fromfiledate | Sort-Object fromfiledate
我们采用每个文本文件的基本名称,并将其从第一个下划线开始分成两部分。然后,我们使用 TryParseExact
尝试将 "date" 字符串转换为 "yyyyMMdd_hhmmss" 的格式。由于我们使用 TryParseExact
如果我们在解析日期时遇到问题,那么代码将继续。
示例输出
Name FromFileDate
---- ------------
myfile_20150812_030949.txt 8/12/2015 3:09:49 AM
myfile_20150813_040949.txt 8/13/2015 4:09:49 AM
files.txt Could not Parse
如果您不希望输出中出现错误数据,一个简单的 Where-Object{$_.fromfiledate -is [datetime]}
将删除这些条目。