Move Files older then 31 days to another drive

大城市里の小女人 提交于 2019-12-20 12:24:33

问题


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
  }
}

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 ?


回答1:


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:\target"

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




回答2:


Also be aware of hidden files, try adding -Force to Get-ChildItem



来源:https://stackoverflow.com/questions/5912051/move-files-older-then-31-days-to-another-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!