psdrive路径仍然太长 [英] psdrive path still too long

查看:93
本文介绍了psdrive路径仍然太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to delete files older then x days so, knowing that we use paths longer than 260 characters, I wrote the following as a workaround:

$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 
}

现在,问题在于,即使以这种方式,当我执行搜索时,我也会得到以下信息:

Now, the problem is that even in this way, when I perform a search I've got the following:

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:\"开始.

It looks like the path it's still starting at "\very.long.path\Files" while I would expect for it to start at "TEMP:\".

有人可以帮忙吗?我想念什么?

Can someone help? What am I missing?

使用替代而不是New-PSDrive修复

fixed using subst instead of New-PSDrive

推荐答案

如果要使用New-PSDrive,则需要使用-Persist swicth参数以绕过字符长度限制.然后,它将像映射的驱动器一样完全起作用.另外,由于它现在已映射,因此您将需要使用单个字母字符作为驱动器字母,并记住在完成操作后将其删除.

If you are going to use New-PSDrive you need to use the -Persist swicth parameters in order to bypass the character length limitation. That will act exactly then like a mapped drive. Also since it is mapped now you will need to use single alphabetical characters for the drive letters and remember to remove them when you are done.

对于免费的驱动器号,我这样做:

For a free drive letter I do this:

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

其他阅读内容:

我对此行为有疑问,这是我应得的.我在我的问题中提到了这个问题:

I had an issue with this behavior which I bountied for. I covered the issue in my question: How can I query a temporary PS-Drive while returning files with a name relative to the drive? and the answer explains why what we were trying to do was folly.

这篇关于psdrive路径仍然太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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