将 31 天之前的文件移动到另一个驱动器 [英] Move Files older then 31 days to another drive

查看:23
本文介绍了将 31 天之前的文件移动到另一个驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:	arget" -force -ErrorAction:SilentlyContinue
  }
}

源目录中的文件是 2-3 年以前的文件,但是当我运行脚本时,没有任何内容移动到目标目录?!怎么了?

in the source directory are files that are older than 2-3 years, but when i run the script nothing moves to the target directory ?! whats wrong ?

推荐答案

我不知道这是否有很大的不同,但与 $.它必须是 $_.

I don't know if this makes much of a difference, but rather than $. it needs to be $_.

我试过这个脚本,它对我来说很好:

I tried this script and it works fine for me:

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

请注意,您不需要 foreach 循环,因为对象将被管道化"到 move-item 命令中

Notice you don't need a foreach loop because the objects will be "piped" into the move-item command

这篇关于将 31 天之前的文件移动到另一个驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆