2017-03-09 62 views
0

我想將文件移動到當前月份的日期差異條件和文件的LastWriteTime的另一個位置。使用PowerShell移動基於日期差異的文件

我上2nd March 2017運行下面的代碼和它的移動Dec-16文件,但只有第一個和Dec-16文件第2日期還有的Dec-16個月多於10個文件。

Jan-17Feb-17文件不移動,這是很好的。理想情況下,它不應該移動Dec-16文件,因爲我寫的條件爲$Now.AddMonths(-3)

這意味着它的檢查日期以及我不想要的。代碼只應檢查月份並相應地移動文件。假設日期可以是任何事情,如果我在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"} 

任何人都可以幫助我嗎?

回答

2

您可以將您的$Lastwrite明確設置爲目標月份第一天的午夜。

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

完美....我只是試過,它的工作。謝謝 :) –