powershell - 比较LastWriteTime和CreationTime

标签 powershell

如果创建和写入文件一次,则我希望LastWriteTime与CreationTime相同。这段代码似乎表明这是不正确的。这些时间戳存储的精度更高吗? ToString和比较是唯一的方法吗?

PS 09:30  C:\src\t
>Get-ChildItem .\uu.txt | Select-Object -Property Name,LastWriteTime,CreationTime

Name   LastWriteTime       CreationTime
----   -------------       ------------
uu.txt 2019-08-09 09:23:25 2019-08-09 09:23:25


PS 09:30  C:\src\t
>Get-ChildItem .\uu.txt | ForEach-Object { $_.LastWriteTime -eq $_.CreationTime }
False

更新:

我最终得到了以下代码。如果LastWriteTime与CreationTime之间的差异小于一秒,则假定该文件自创建以来尚未更新。
$CloseEnough = New-Timespan -Seconds 1
Get-ChildItem -File -Path 'C:\src\t' |
    Where-object { ($_.LastWriteTime - $_.CreationTime) -lt $CloseEnough }

最佳答案

即使写入文件也需要一些时间,因此必须有所不同。

检查:

> Get-Item .\uu.txt | ForEach-Object { $_.LastWriteTime - $_.CreationTime}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 2
Ticks             : 27616
TotalDays         : 3,1962962962963E-08
TotalHours        : 7,67111111111111E-07
TotalMinutes      : 4,60266666666667E-05
TotalSeconds      : 0,0027616
TotalMilliseconds : 2,7616

奇怪的是,以上时间是在Mac主机上的VirtualBox VM的Win10pro中写入Gbit网络FreeNAS共享的。

本地Win10 Pc写入本地RamDisk / SSD的速度要慢得多。

关于powershell - 比较LastWriteTime和CreationTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57432314/

相关文章:

powershell - 防止在启动Docker容器时Gitlab将-NoProfile添加到PowerShell

excel - Powershell等待Excel中执行宏

powershell - 如何在NSIS中调用PowerShell

powershell - 在Powershell中检测跨应用程序的按键

powershell - 如何传递凭据来重命名命令?

powershell - 如何使用Powershell检查硬盘驱动器是eide还是sata

powershell - CNAMES,主机文件和PowerShell远程处理

powershell - VisualSVN Server PowerShell配置

shell - Powershell:如何在无休止的脚本行之后继续运行脚本?

windows - powershell 脚本中的程序集