PowerShell脚本将文件和文件夹(包括子文件夹)从一个位置移动到x天以外的另一个位置

use*_*332 15 powershell powershell-2.0 powershell-1.0 powershell-3.0

我开发了一个PowerShell脚本,它的工作非常好.唯一的挑战是子文件夹中的文件没有移动到目标.

get-childitem -Path "\\servername\location" |
    where-object {$_.LastWriteTime -lt (get-date).AddDays(-31)} |
    move-item -destination "C:\Dumps"
Run Code Online (Sandbox Code Playgroud)

我无法进一步自定义脚本.

Ans*_*ers 16

不要浪费你的时间试图robocopy在PowerShell中重新发明.

robocopy \\servername\location C:\Dumps /e /mov /minage:31
Run Code Online (Sandbox Code Playgroud)

  • robocopy通过逐位复制文件然后将其删除**来完成移动**.而只是快速移动它.因此要慢得多.无法关闭此行为. (3认同)
  • @AnsgarWiechers在文件夹中移动相同的文件系统时会发生此行为.因为afaik没有别的东西使用这种奇怪而缓慢的方法,值得一提 (2认同)

Mus*_*idi 12

使用命令-Recurse上的选项Get-ChildItem来访问子文件夹中的文件,然后通过将集合管道分别移动每个文件Move-Item

Get-ChildItem -Path "C:\Test" -Recurse |
  Where-Object {$_.LastWriteTime -lt (Get-date).AddDays(-31)} |
  Move-Item -destination "C:\Dumps"
Run Code Online (Sandbox Code Playgroud)

这是一个截图:

截图

  • 这不会保留文件夹结构 (2认同)

Ste*_*ney 7

简化上述
robocopy A:\ B:\ /MIR /minage:31
A:\是您的来源B:\是您的目的地