【问题标题】:Comparison of date in PowerShell in commandsPowerShell中命令中的日期比较
【发布时间】:2018-02-16 17:56:52
【问题描述】:

我正在使用获取子项来获取文件,我想获取上次修改日期/创建日期与今天日期匹配的文件

PS E:\> Get-Childitem –Path 'E:\utility\sysout' -Include *DBA_M_MNT_BACKUP_system* -File -Recurse -ErrorAction SilentlyContinue

输出我得到了

    Directory: E:\utility\sysout


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2/16/2018   2:00 AM       4709 TEDM_DBA_M_MNT_BACKUP_System_Databases_021620180200AM.txt

我想查看 lastwritetime 我希望日期与当前日期匹配 喜欢get-date.day get-date.month get-date.year 我需要单独拿起有没有办法可以直接比较 lastwritetime (仅日期)与当前日期 并仅在日期与当前日期匹配时给出命令输出

【问题讨论】:

    标签: powershell


    【解决方案1】:

    是的,确实有办法。你在寻找这样的东西吗 -

    $File = Get-Childitem –Path 'E:\utility\sysout' -Include *DBA_M_MNT_BACKUP_system* -File -Recurse -ErrorAction SilentlyContinue
    $timediff = (Get-Date) - $File.LastWriteTime
    

    您可以使用$timediff.Days$timediff.Hours 等访问时差。当您仅获得一个文件作为输出时,上述方法有效。但是,如果您在$File 中获得多个文件,那么您可能希望像$File[0].LastWriteTime$File[1].LastWriteTime$File[2].LastWriteTime 等一样使用它。或者您可以简单地使用Foreach 循环,根据您的要求选择适合您的任何内容.

    【讨论】:

    • 我使用了类似的东西,因为我想比较年份月份和日期,我不必花时间 $File=Get-Childitem –Path 'E:\utility\sysout' -Include DBA_M_MNT_BACKUP_system -File -Recurse -ErrorAction SilentlyContinue $timediffyear = (Get-Date).year - $File.LastWriteTime.year $timediffmonth = (Get-Date).month - $File.LastWriteTime.month write-host $timediff write-host $timediffmonth,对于几个月的时差,它没有显示任何我想简单地显示文件名,如果 lastwritetime 匹配当前日期,否则错误
    • (Get-Date).Month - ($File.LastWriteTime).Month 应该返回您的月差。如果没有区别,它将返回0
    • $File=Get-Childitem –Path 'E:\utility\sysout' -包括 DBA_M_MNT_BACKUP_system -File -Recurse -ErrorAction SilentlyContinue $timediffyear = (Get-Date)。 year - $File.LastWriteTime.year $timediffmonth = (Get-Date).month - ($File.LastWriteTime).month write-host $timediff write-host $timediffmonth 输出 PS E:\> e:\utility\example。 ps1 0 不显示月差
    • 最终得到了时间差异的答案并检查了 $File=Get-Childitem –Path 'E:\utility\sysout' -Include DBA_M_MNT_BACKUP_system -File -Recurse -ErrorAction SilentlyContinue $ timediffyear = (Get-Date).year - $File.LastWriteTime.year $timediffmonth = (Get-Date).month - ($File.LastWriteTime).month $timediffday = (Get-Date).day - ($File. LastWriteTime).day write-host $timediffyear write-host $timediffmonth write-host $timediffday
    【解决方案2】:

    代码

    Get-ChildItem -Path C:\temp |
        Where-Object {
            $_.LastWriteTime -ge ((Get-Date).Date) -and
            $_.LastWriteTime -lt ((Get-Date).Date).AddDays(+1)
        }
    

    这是如何工作的?

    它会查看LastWriteTime 是否大于今天的“开始”和明天的“开始”,其中“开始”是00:00:00 的时间

    插图

    Write-Host (Get-Date)
    Write-Host (Get-Date).Date
    Write-Host ((Get-Date).Date).AddDays(+1)
    

    结果

    16/02/2018 08:03:08
    16/02/2018 00:00:00
    17/02/2018 00:00:00
    

    【讨论】:

      猜你喜欢
      • 2015-08-11
      • 1970-01-01
      • 2019-03-06
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 2017-01-19
      相关资源
      最近更新 更多