2011-05-06 73 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

相关问题