psdrive路径仍然太长

时间:2016-02-26 14:21:46

标签: powershell path

我必须删除早于x天的文件,因此,我知道我们使用超过260个字符的路径,我写了以下作为解决方法:

$path= "\\very.long.path\Files\P\still\_veryLong\more_stuff\never_finish\Local\Development\really_longher_tha_260" 
if (-not (Test-Path TEMP:)) {
    echo "Mounting virtual drive TEMP: ..."
    New-PSDrive -Name TEMP -PSProvider FileSystem -Root $path 
}

现在,问题是,即使这样,当我执行搜索时,我还有以下内容:

Get-ChildItem TEMP: -Recurse -Force | Where-Object {
  !$_.PSIsContainer -and $_.CreationTime -lt $limit
}
Get-ChildItem : 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 C:\UserTemp\WorkStuff\jkCleanOldFiles.ps1:19 char:13
+ $allFiles = Get-ChildItem . -Recurse
+             ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ReadError: (\\very.long.path\Files..lpha-9-stable- :String) 
+ Get-ChildItem], PathTooLongException
+ FullyQualifiedErrorId : irIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

从这一行:

+ CategoryInfo          : ReadError: (\\very.long.path\Files...lpha-9-stable- :String)

看起来它仍然是从“\ very.long.path \ Files”开始的路径,而我希望它从“TEMP:\”开始。

有人可以帮忙吗?我错过了什么?

编辑:使用subst而不是New-PSDrive修复

3 个答案:

答案 0 :(得分:1)

如果您要使用New-PSDrive,则需要使用-Persist swicth参数以绕过字符长度限制。这将完全像一个映射驱动器。此外,由于现在已映射,因此您需要为驱动器号使用单个字母字符,并在完成后记得将其删除。

对于免费的车道信,我这样做:

# Get a free drive letter for create temporary persistent drives
$freeDrive = [char[]](68..90) | Where-Object{!(Get-PSDrive $_ -ErrorAction SilentlyContinue)} | Select -First 1

进一步阅读:

我遇到了这个行为的问题,我为此付出了代价。我在问题中提到了这个问题:How can I query a temporary PS-Drive while returning files with a name relative to the drive?,答案解释了为什么我们要做的事情是愚蠢的。

答案 1 :(得分:0)

如果是本地路径,您可以尝试通过\\?\ C:\ very \ long \ path访问它。 这样你可能根本不需要安装任何东西。

答案 2 :(得分:0)

使用net use代替New-PSDrive。但是,仍然需要UNC路径短于248个字符,因此使用正则表达式修剪它:

$path= '\\very.long.path\Files\P\still\_ve...ent\really_longher_tha_260'
$parent = $path -replace '^(.{1,248})\\.*', '$1'

net use t: "$parent"

Get-ChildItem t: -Recurse -Force | Where-Object {
  -not $_.PSIsContainer -and
  $_.CreationTime -lt $limit
}

net use t: /d

使用subst代替net use,可以采用相同的方式处理过长的本地路径。

如果映射驱动器下面的剩余路径仍然超过248个字符,请重复此过程。

用一个线索击中那些负责这些疯狂长路径的人。直到他们昏倒。

相关问题