【问题标题】:Moving file based on date difference using powershell使用powershell根据日期差异移动文件
【发布时间】:2017-03-09 09:35:10
【问题描述】:

我想将文件移动到具有当前月份日期差异条件和文件LastWriteTime 的另一个位置。

我在2nd March 2017 及其移动的Dec-16 文件上运行以下代码,但只有Dec-16 文件的第一个和第二个日期,并且Dec-16 个月的文件超过10 个。

Jan-17Feb-17 文件没有移动,这很好。理想情况下,它也不应该移动 Dec-16 文件,因为我将条件写为 $Now.AddMonths(-3)

这意味着它的检查日期也是我不想要的。代码应该只检查月份并相应地移动文件。让日期可以是任何东西,如果我在三月中旬以$Now.AddMonths(-3) 运行命令,那么文件应该移动到30th November 2016 并保持1st Dec-16 直到日期不变。

$Now = Get-Date
$Lastwrite = $Now.AddMonths(-3)
$childItems = Get-ChildItem $folderNameFull -Include .txt,.xml,.csv,.xls -Recurse | where {$_.LastwriteTime -lt "$Lastwrite"}

谁能帮我解决这个问题?

【问题讨论】:

    标签: windows file powershell date automation


    【解决方案1】:

    您可以将$Lastwrite 明确设置为目标月份第一天的午夜。

    $Lastwrite = (Get-Date -Day 1).AddMonths(-3).Date
    $childItems = Get-ChildItem $folderNameFull -Include .txt,.xml,.csv,.xls -Recurse | where {$_.LastwriteTime -lt $Lastwrite}
    

    【讨论】:

      猜你喜欢
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多