【问题标题】:Remove-Item - directory name must be less than 248Remove-Item - 目录名称必须小于 248
【发布时间】:2015-04-03 07:23:15
【问题描述】:

这让我很沮丧,所以我向你们寻求帮助..

我有一百万个(有点夸张)文件夹要删除,我可以一个一个地从 Windows 资源管理器中删除 YAY...但我想在 Powershell(版本 4)中编写脚本

使用此代码:

$Path = '\\verylonguncpath\plussomemore\'
Remove-Item -Path $Path -Recurse -Force -Confirm:$False

我得到错误:

Remove-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters. At line:5 char:1 + Remove-Item -Path $Path -Recurse -Force -Confirm:$False + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (\\verylongpath\plussomemore:String) [Remove-Item], PathTooLongException + FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

如果$Path 不是很长,它可以工作......

我什至尝试映射到一个字母然后删除内容,但我遇到了同样的问题?

显然它的 Microsoft 或 Powershell 是我一直在阅读的问题?

可能是用户错误:\

任何建议都会很棒,我真的很想只使用 MS 产品,但第三方程序会很好(在 Powershell 脚本中使用)..

谢谢帕夫。

【问题讨论】:

标签: powershell scripting powershell-ise powershell-4.0


【解决方案1】:

这行得通:

Cmd /C "rmdir /S /Q $myDir"

只需确保使用引号创建变量并运行上述命令即可。

$mydir = "\pathToDelete\"

【讨论】:

    【解决方案2】:
    function Remove-FolderDeep([string]$folder){
    $SubFolderList = Get-ChildItem -Path $folder -Directory -ErrorAction Ignore
    for ($i = 0; $i -lt $SubFolderList.Length; $i++) {
        # rename the subfolder to avoid long file name
        Rename-Item -Path ($folder + "\" + $SubFolderList[$i]) -NewName ($folder + "\" + $i)
        Remove-FolderDeep -folder ($folder + "\" + $i)
    }
    # remove the folder 
    Remove-Item $folder -Force -Recurse
    

    }

    【讨论】:

      【解决方案3】:

      @campbell.rw

      谢谢!

      现在我可以轻松删除这些文件夹了 :) 耶

      Pavle 给你 51 个假网点

      所以答案在下面,它确实有效……对我来说效果很好。

      Delete directory regardless of 260 char limit

      Cmd /C "rmdir /S /Q $myDir"

      【讨论】:

      • 许多人(现在包括我在内)遗憾地发现这并不适用于所有情况。
      • 如果文件夹有空间,它将停止......他们可能是一个更好的方法,但我这样做......$path = """$path""" 它有效因为您在引号中输入了路径。告诉我进展如何。
      • 试过了,无法删除整个node-modules文件夹
      猜你喜欢
      • 1970-01-01
      • 2011-02-01
      • 2014-08-07
      • 2019-07-08
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2016-03-21
      • 1970-01-01
      相关资源
      最近更新 更多