CreationTime生成错误的日期

时间:2019-02-06 01:29:06

标签: powershell

我正在检查是否使用以下脚本最近创建了文件:

$today = (Get-Date).Date
$file = Get-ChildItem -Filter *.csv | where { $_.CreationTime.Date -eq $today}

if ($fileCreated) {
    Write-Host "`r`n << $file >> generated in [ $((Get-Location).Path) ]"
} else {
    Write-Host "`r`n file NOT generated!"
}

昨天它运行良好。

由于某种原因,$file.CreationTime每次都会输出

CreationTime

例如,即使文件是使用$today创建的,

today

如果我对此进行测试:

$file = Get-ChildItem -Filter *.csv
Write-Host ($file.CreationTime.ToString('M/d/yyyy hh:mm:ss tt'))
Write-Host "$file generated in [ $((Get-Location).Path) ]"

我取回了文件并将其放在正确的位置,因此我确定可以正确找到文件。但是,现在在我添加ToString之后,创建时间总是这样。

CreationTimeToString

我不知道昨天的脚本怎么可能完美运行,而现在突然又无法按预期工作了……我该怎么做才能使正确的CreationTime与{{ 1}}?

1 个答案:

答案 0 :(得分:0)

发生这种情况的原因是,CreationTime是创建文件的时间。但是,由于没有重新创建该文件,换句话说,每次我的脚本假定生成csv文件时,它都被修改了,因此创建时间当然不会改变。

根据@Lee_Dailey的建议,更合适的使用方法是LastWriteTime属性。

$file = Get-ChildItem -Filter *.csv | where { $_.LastWriteTime.GetDateTimeFormats()[6] -eq $today}
相关问题