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

大规模文件树中文件与文件夹批量重命名的PowerShell技术问询

问题解答

1. 递归遍历并修复所有嵌套文件/文件夹的可行方案

你写的递归脚本报错核心原因是处理顺序错误:先改当前目录内容,再遍历子目录,一旦父目录被重命名,后续递归调用的子目录路径就会失效。正确逻辑是先递归处理最深层的子目录,再处理当前目录的文件和文件夹,这样父目录重命名时,子目录已经处理完毕,不会破坏路径关联。

以下是修正后的稳定递归脚本:

# 定义要处理的根目录
$rootPath = "C:\Path\To\Your\Directory"

# 递归处理函数:先处理子目录,再处理当前目录内容
function Rename-Recursively {
    param(
        [string]$currentPath
    )

    # 第一步:递归处理所有子目录(从最深层开始)
    Get-ChildItem -Path $currentPath -Directory | ForEach-Object {
        Rename-Recursively -currentPath $_.FullName
    }

    # 第二步:处理当前目录下的所有文件和文件夹
    Get-ChildItem -Path $currentPath | ForEach-Object {
        $item = $_
        # 匹配带"- modify"后缀的名称
        if ($item.Name -like "*- modify*") {
            # 生成新名称:移除后缀,还原符号编码
            $newName = $item.Name -replace "- modify", "" -replace "0023", "#"
            $newFullPath = Join-Path -Path $currentPath -ChildPath $newName

            # 执行重命名,遇错终止方便排查(可改为Continue忽略错误)
            Rename-Item -Path $item.FullName -NewName $newFullPath -ErrorAction Stop

            # 输出日志
            $itemType = if ($item.PSIsContainer) { "目录" } else { "文件" }
            Write-Host "已重命名$itemType : $($item.FullName) -> $newFullPath"
        }
    }
}

# 启动处理
Rename-Recursively -currentPath $rootPath

关键优化点:

  • 处理顺序反转:从最深层子目录开始处理,避免父目录重命名导致子路径失效
  • 管道化处理:用ForEach-Object替代传统foreach,处理95万级文件时性能更稳定
  • 错误控制:-ErrorAction Stop让错误及时暴露,方便定位问题

2. 超255字符路径的处理方案

PowerShell默认受Windows传统260字符路径限制,但有两种可靠的解决方式:

方式1:系统级启用长路径支持(Windows 10 1607+/Server 2016+)

  1. 组策略设置:打开gpedit.msc,导航到计算机配置 > 管理模板 > 系统 > 文件系统 > NTFS,启用「启用Win32长路径」
  2. 注册表设置(无组策略编辑器时用):打开regedit,定位到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem,创建/修改DWORD值LongPathsEnabled为1
  3. 重启系统生效

方式2:用\\?\前缀绕过限制

给绝对路径添加\\?\前缀,直接调用Win32 API绕过路径长度限制,修改脚本根路径即可:

$rootPath = "\\?\C:\Path\To\Your\Directory"

注意:该前缀仅支持绝对路径,不能包含..这类相对路径符号。

适配长路径的最终脚本

结合两种方案,以下脚本可以完美处理长路径场景:

# 管理员权限下自动启用长路径支持(静默忽略已设置的情况)
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -Name LongPathsEnabled -Value 1 -ErrorAction SilentlyContinue

# 带\\?\前缀的根路径
$rootPath = "\\?\C:\Path\To\Your\Directory"

function Rename-Recursively {
    param(
        [string]$currentPath
    )

    Get-ChildItem -Path $currentPath -Directory | ForEach-Object {
        Rename-Recursively -currentPath $_.FullName
    }

    Get-ChildItem -Path $currentPath | ForEach-Object {
        $item = $_
        if ($item.Name -like "*- modify*") {
            $newName = $item.Name -replace "- modify", "" -replace "0023", "#"
            $newFullPath = Join-Path -Path $currentPath -ChildPath $newName

            # 给新路径也添加\\?\前缀(保持一致性)
            if ($currentPath -match "^\\\\\?\\") {
                $newFullPath = "\\?\$newFullPath"
            }

            Rename-Item -Path $item.FullName -NewName $newFullPath -ErrorAction Stop

            $itemType = if ($item.PSIsContainer) { "目录" } else { "文件" }
            Write-Host "已重命名$itemType : $($item.FullName) -> $newFullPath"
        }
    }
}

Rename-Recursively -currentPath $rootPath

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 12:57:04