2011-05-06 33 views
10
Function Move { 
    #Moves all files older than 31 days old from the Source folder to the Target 
    Get-Childitem -Path "E:\source" | Where-Object { $_.LastWriteTime -lt (get-date).AddDays(-31)} | 
    ForEach { 
    Move-Item $_.FullName -destination "F:\target" -force -ErrorAction:SilentlyContinue 
    } 
} 

在源目錄中是大於2 - 3年的文件,但是當我運行腳本時,沒有任何操作移動到目標目錄?怎麼了 ?將文件大於31天移動到另一個驅動器

+0

如果您取出ErrorAction,是否會產生任何錯誤? – 2011-05-06 13:25:24

+0

您的文件是否正確地存在於'E:\ source'或其子目錄中?在後一種情況下,使用'Get-Childitem -Recurse' – 2011-05-06 15:54:53

+1

FYI,這將不會移動子目錄中的文件。 – JasonMArcher 2011-05-06 15:56:20

回答

16

我不知道這是否有很大的區別,而不是$。它需要是$ _。

我想這個腳本,它爲我工作得很好:

get-childitem -Path "E:\source" | 
    where-object {$_.LastWriteTime -lt (get-date).AddDays(-31)} | 
    move-item -destination "F:\target" 

通知你不需要foreach循環,因爲對象將是「管道」到移動項目命令

+0

get-currentitem你的意思Get-Childitem? – JPBlanc 2011-05-06 14:39:00

+0

爲什麼它不適合我......非常奇怪。我想過要避免問題的foreach,因爲我們在一些導演中有20k +文件.... – 2011-05-06 15:13:55

+0

你有一些想法嗎? – 2011-05-06 15:14:43

4

而且請注意隱藏文件,請嘗試將-Force添加到Get-ChildItem

相關問題