You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell批量将二级子文件夹移动至三级子文件夹

批量移动二级子文件夹到同目录Archive下作为三级子目录

当前目录结构

F:\Doe, Jane\Archive\

F:\Doe, Jane\A Files\1 Files\file1.docx
F:\Doe, Jane\A Files\2 Files\file1.docx
F:\Doe, Jane\A Files\3 Files\file1.docx

F:\Doe, Jane\B Files\1 Files\file1.docx
F:\Doe, Jane\B Files\2 Files\file1.docx
F:\Doe, Jane\B Files\3 Files\file1.docx

F:\Public, John Q\Archive

F:\Public, John Q\A Files\1 Files\file1.docx
F:\Public, John Q\A Files\2 Files\file1.docx
F:\Public, John Q\A Files\3 Files\file1.docx

F:\Public, John Q\B Files\1 Files\file1.docx
F:\Public, John Q\B Files\2 Files\file1.docx
F:\Public, John Q\B Files\3 Files\file1.docx

期望目录结构

F:\Doe, Jane\Archive\A Files\1 Files\file1.docx
F:\Doe, Jane\Archive\A Files\2 Files\file1.docx
F:\Doe, Jane\Archive\A Files\3 Files\file1.docx

F:\Doe, Jane\Archive\B Files\1 Files\file1.docx
F:\Doe, Jane\Archive\B Files\2 Files\file1.docx
F:\Doe, Jane\Archive\B Files\3 Files\file1.docx

F:\Public, John Q\Archive\A Files\1 Files\file1.docx
F:\Public, John Q\Archive\A Files\2 Files\file1.docx
F:\Public, John Q\Archive\A Files\3 Files\file1.docx

F:\Public, John Q\Archive\B Files\1 Files\file1.docx
F:\Public, John Q\Archive\B Files\2 Files\file1.docx
F:\Public, John Q\Archive\B Files\3 Files\file1.docx

尝试的代码及错误

尝试用通配符匹配源和目标路径,但报错:

测试代码

$source = "F:Test\*\Client Uploads\"
$destination = "F:Test\*\Archive\"

Get-ChildItem -Path $source -Directory | 
    Move-Item -Destination $destination -Force -Verbose

错误信息

VERBOSE: Performing the operation "Move Directory" on target "Item: F:\Test\Doe, Jane\Client Uploads Destination:
F:\Test\*\Archive\".
Move-Item : Illegal characters in path.
At line:5 char:5
+     Move-Item -Destination $destination -Force -Verbose
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (F:\Test\Doe, Jane\Client Uploads:DirectoryInfo) [Move-Item], Argu
   mentException
    + FullyQualifiedErrorId : MoveDirectoryItemArgumentError,Microsoft.PowerShell.Commands.MoveItemCommand

问题核心:目标路径中的*通配符无法和源路径的*动态匹配,PowerShell的Move-Item不支持这种模糊对应,必须为每个文件夹生成明确的目标路径。

解决方案

遍历每个用户根目录,针对每个目录单独处理移动逻辑,确保源和目标路径一一对应:

# 替换为实际的顶层目录,测试环境可改为"F:\Test"
$rootDir = "F:\"

# 遍历所有用户目录(如Doe, Jane、Public, John Q)
Get-ChildItem -Path $rootDir -Directory | ForEach-Object {
    $userDir = $_.FullName
    # 定义需要移动的二级文件夹名称
    $foldersToMove = @("A Files", "B Files")
    $archiveDir = Join-Path -Path $userDir -ChildPath "Archive"

    # 确保Archive目录存在,不存在则自动创建
    if (-not (Test-Path -Path $archiveDir)) {
        New-Item -Path $archiveDir -ItemType Directory | Out-Null
    }

    # 逐个移动目标文件夹
    foreach ($folder in $foldersToMove) {
        $sourcePath = Join-Path -Path $userDir -ChildPath $folder
        # 检查源文件夹是否存在
        if (Test-Path -Path $sourcePath -PathType Container) {
            $destPath = Join-Path -Path $archiveDir -ChildPath $folder
            Move-Item -Path $sourcePath -Destination $destPath -Force -Verbose
        }
    }
}

代码说明

  1. 指定顶层目录后,自动遍历其中的用户目录
  2. 对每个用户目录,先确认Archive文件夹存在,不存在则创建
  3. 遍历要移动的A Files、B Files,检查源路径有效后,移动到该用户目录的Archive文件夹下
  4. -Force参数会覆盖目标路径中已存在的同名文件夹,不需要可删除

内容的提问来源于stack exchange,提问作者JayWalk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 20:00:16