现在我有这段代码:
$items = @()
$OutputFilePath = "c:\csv\text1.csv"
dir -Force -Recurse | foreach {
$FullName = $_.FullName
$Creation = $_.CreationTimeUtc
$Modified = $_.LastWriteTimeUtc
$Accessed = $_.LastAccessTimeUtc
$Size = $_.Length
$Atributes = $_.Attributes
$i = New-Object -TypeName psobject
$i | Add-Member -MemberType NoteProperty -Name FullName -Value $FullName
$i | Add-Member -MemberType NoteProperty -Name CreatedDateUtc -Value $Creation
$i | Add-Member -MemberType NoteProperty -Name CreatedTimeUtc -Value $Creation.TimeOfDay
$i | Add-Member -MemberType NoteProperty -Name CreatedTicksUtc -Value $Creation.Ticks
$i | Add-Member -MemberType NoteProperty -Name ModifiedDateUtc -Value $Modified
$i | Add-Member -MemberType NoteProperty -Name ModifiedTimeUtc -Value $Modified.TimeOfDay
$i | Add-Member -MemberType NoteProperty -Name ModifiedTicksUtc -Value $Modified.Ticks
$i | Add-Member -MemberType NoteProperty -Name AccessedDateUtc -Value $Accessed
$i | Add-Member -MemberType NoteProperty -Name AccessedTimeUtc -Value $Accessed.TimeOfDay
$i | Add-Member -MemberType NoteProperty -Name AccessedTicksUtc -Value $Accessed.Ticks
$i | Add-Member -MemberType NoteProperty -Name Size -Value $Size
$i | Add-Member -MemberType NoteProperty -Name Atributes -Value $Atributes
$items += $i
}
$FileExists = Test-Path $OutputFilePath
if ($FileExists -eq $False) {
$items | Export-Csv -Path $OutputFilePath -NoClobber -Encoding UTF8
} else {
[System.Windows.Forms.MessageBox]::Show("The output file already exists. Please delete or rename it to continue." ,"Error", 0, [System.Windows.Forms.MessageBoxIcon]::Error)
}
它为我提供了文件夹和子文件夹中所有对象的UTC时间戳列表。 (我发现UTC时间戳比非UTC时间戳更有用,因为我致力于虚拟现实天气时光倒流服务的概念,我理解夏令时开始和结束时的错误是不可接受的所以我说这些错误是在其他情况下也是不可接受的。)我缺少的是父文件夹本身的时间戳。
我怎样才能包含父文件夹的时间戳?
答案 0 :(得分:3)
在当前目录中添加Get-Item
:
$(Get-Item .; Get-ChildItem . -Recurse -Force) | ForEach-Object {
...
}
答案 1 :(得分:3)
除了Ansgar Wiechers解决问题的答案之外:
$OutputFilePath = "c:\csv\text1.csv"
if (Test-Path $OutputFilePath) {
[System.Windows.Forms.MessageBox]::Show(
"The output file already exists. Please delete or rename it to continue.",
"Error",
0,
[System.Windows.Forms.MessageBoxIcon]::Error
)
} else {
$(gi .; dir -Force -Recurse) | %{
New-Object PSObject -Property @{
FullName = $_.FullName
CreatedDateUtc = $_.CreationTimeUtc
CreatedTimeUtc = $_.CreationTimeUtc.TimeOfDay
CreatedTicksUtc = $_.CreationTimeUtc.Ticks
ModifiedDateUtc = $_.LastWriteTimeUtc
ModifiedTimeUtc = $_.LastWriteTimeUtc.TimeOfDay
ModifiedTicksUtc = $_.LastWriteTimeUtc.Ticks
AccessedDateUtc = $_.LastAccessTimeUtc
AccessedTimeUtc = $_.LastAccessTimeUtc.TimeOfDay
AccessedTicksUtc = $_.LastAccessTimeUtc.Ticks
Size = $_.Length
Atributes = $_.Atributes
}
} | Export-Csv -Path $OutputFilePath -NoClobber -Encoding UTF8
}
我已经使用别名(gi
Get-Item
和%
foreach
),因为你喜欢它们。