删除目录,而不考虑260个字符的限制 [英] Delete directory regardless of 260 char limit

查看:88
本文介绍了删除目录,而不考虑260个字符的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的脚本,以在一定天数后删除USMT迁移文件夹:

I'm writing a simple script to delete USMT migration folders after a certain amount of days:

## Server List ##
$servers = "Delorean","Adelaide","Brisbane","Melbourne","Newcastle","Perth"

## Number of days (-3 is over three days ago) ##
$days = -3

$timelimit = (Get-Date).AddDays($days)

foreach ($server in $servers)
{
    $deletedusers = @()
    $folders = Get-ChildItem \\$server\USMT$ | where {$_.psiscontainer}
    write-host "Checking server : " $server
    foreach ($folder in $folders) 
    {
        If ($folder.LastWriteTime -lt $timelimit -And $folder -ne $null)
        {
            $deletedusers += $folder
            Remove-Item -recurse -force $folder.fullname
        }
    }
        write-host "Users deleted : " $deletedusers
        write-host
}

但是我一直打着可怕的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.

However I keep hitting the dreaded 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.

我一直在寻找解决方法和替代方法,但是它们都围绕着我,关心着文件夹中的内容.

I've been looking at workarounds and alternatives but they all revolve around me caring what is in the folder.

我希望有一个更简单的解决方案,因为我真的不关心文件夹中是否标有删除的内容.

I was hoping for a more simple solution as I don't really care about the folder contents if it is marked for deletion.

除了 Remove-Item -recurse 以外,是否还有其他本机Powershell cmdlet可以完成我要执行的操作?

Is there any native Powershell cmdlet other than Remove-Item -recurse that can accomplish what I'm after?

推荐答案

这是PowerShell的已知限制.解决方法是使用dir cmd(抱歉,这是事实).

This is a known limitation of PowerShell. The work around is to use dir cmd (sorry, but this is true).

http://asysadmin.tumblr.com/post/17654309496 /powershell-path-length-limitation

或如AaronH提到的那样,在此示例中使用\?\语法删除构建

or as mentioned by AaronH answer use \?\ syntax is in this example to delete build

dir -Include build -Depth 1 | Remove-Item -Recurse -Path "\\?\$($_.FullName)"

这篇关于删除目录,而不考虑260个字符的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆