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

含双层方括号[[...]]的目录/文件无法删除或重命名求助

问题:清理含双层方括号的NAS用户配置文件目录失败

我需要定期清理中央NAS上的旧Windows用户配置文件目录,近期遇到路径包含双层方括号(来自Macromedia导入)的目录,示例路径如下:

\server.ad.local\HOME1\Username\System\Anwendungsdaten\Macromedia\FlashPlayer\SharedObjects\G5EUZBX2\www.daserste.de[[IMPORT]]\players.edgesuite.net\flash\plugins\osmf\advanced-streaming-plugin\v3.4\osmf2.0\AkamaiAdvancedStreamingPlugin.swf\HDCore.sol

尝试以下PowerShell自动清理方法均失败:

  • 替换方括号并重命名文件夹
  • 使用Remove-Item的-LiteralPath参数
  • 使用Scripting.FileSystemObject删除

每次执行均报错:无法删除元素...,找不到路径“HDCore.sol”的一部分。调用过自定义的removeRecursive函数,也试过Remove-Item -LiteralPath SomePath -Recursive -Force,都没有效果。

附上最后使用的几个函数代码:

Function RemoveChar
{
    Param ($Path)
    $Pattern = '#|&|%|\[{1,}|\]{1,}|\^|\s|\.{2,}'
    if ($Path -imatch $Pattern){
        Rename-Item -Path $Path -NewName ($Path -replace $Pattern,'') -ErrorAction SilentlyContinue
        return ($Path -replace $Pattern,'')
    } else {
        return $Path
    }
}
 
Function Truncate
{
    Param ($Path)
    $total_path_length_threshold = 248  # 目录最大允许字符数(目录248,文件260)
    $characters_to_truncate = 60        # 名称需要截断的字符数,会附加唯一索引
    $virtual_drive = "v:"               # 临时处理必须用"v:"格式
    $collection = cmd /c dir $Path /s /b |? {$_.length -gt $total_path_length_threshold }
    $count_paths = ($collection | measure).count - 1
   
    foreach ($pathlong in $collection) {
        $parent_path = Split-path -path $pathlong
        subst $virtual_drive $parent_path
        $leaf = split-path -leaf $pathlong
        $short_virtual_path = join-path $virtual_drive $leaf
        $item = Get-Item -LiteralPath $short_virtual_path
        if (Test-Path -LiteralPath $item) {
            $filename = $item.name
            $filename_extension = $item.Extension
            $basename = $item.BaseName   
            $basename_length = $basename.length
            $new_index = "X" + $counter + "X"
            $adjusted_characters_to_truncate = $characters_to_truncate + $new_index.length
            if ( $basename_length -gt $adjusted_characters_to_truncate ) {
                $length_to_use = $basename_length - $adjusted_characters_to_truncate
                $new_filename = $basename.substring(0, $length_to_use ) + $new_index + $filename_extension
                $new_path = $parent_path + $new_filename
                $Path = $new_path
                Rename-Item -LiteralPath $short_virtual_path -NewName $new_filename  
            }
        }
        subst v: /d
    }
    return $Path
}
 
Function removeRecursive
{
    param([String] $Path)
    $fso = new-object -com "Scripting.FileSystemObject"
 
    function proc {
         param($folder)
         $folder.Files | foreach-object {
         RemoveChar $_
         }
         $folder.Files | foreach-object {
            $short = $fso.GetFile($_.FullName).ShortPath
            LogWrite "$FullDate : Processing: $short"
            $fso.DeleteFile($short,$true)
         }
        $folder.SubFolders | foreach-object {
        proc $_
        }
    }
   proc $fso.GetFolder($Path)  
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 08:50:27