开发者

Move Files older then 31 days to another drive

开发者 https://www.devze.com 2023-03-03 23:19 出处:网络
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)} |
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 {
    Mov开发者_如何学JAVAe-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 ?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消