将超过 31 天的文件移至另一个驱动器

2024-03-09

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 年,但是当我运行脚本时,没有任何内容移动到目标目录?!怎么了 ?


我不知道这是否有很大的不同,但比 $.它需要是$_。

我尝试了这个脚本,它对我来说效果很好:

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

请注意,您不需要 foreach 循环,因为对象将被“输送”到 move-item 命令中

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将超过 31 天的文件移至另一个驱动器 的相关文章

随机推荐