Powershell 脚本仅将子文件夹中的文件移动到另一个位置,一次一个子文件夹

问题描述

我正在处理一个 PowerShell 脚本,我们需要将子文件夹中的文件移动到另一个位置,一次一个文件夹,即移动子文件夹 1 中的文件,等待 10 分钟,然后将文件从子文件夹 2 移动。 脚本移动所有子文件夹中的文件

Get-ChildItem –Path "C:\PS\"*.HTML -Recurse | Foreach-Object {
    Move-Item $_.FullName -Destination E:\work\tst.txt
}

文件夹结构:

Parent folder
  Child1
     file1.txt
     file2.txt
     file3.txt
Child2
     file1.txt
     file2.txt
     file3.txt
Child3
     file1.txt
     file2.txt
     file3.txt

解决方法

使用 Get-ChildItem 发现每个子文件夹,然后:

Get-ChildItem -Path C:\PS -Directory |ForEach-Object {
  # move files from this folder to destination
  $_ |Get-ChildItem -File |Move-Item -Destination E:\work\ -Force

  # wait 10 minutes
  Start-Sleep -Seconds 600
}