1

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

From this line:

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

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?

EDIT: fixed using subst instead of New-PSDrive

Segolas
  • 575
  • 2
  • 10
  • 26
  • This is related: http://stackoverflow.com/questions/34812349/how-can-i-query-a-temporary-ps-drive-while-returning-files-with-a-name-relative – Matt Feb 26 '16 at 15:03

3 Answers3

1

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

For additional reading:

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.

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119
  • Hi,thanks for the reply. The letter is not a problem as this has to run on severs with a know number of drives mounted on. I think your solution may be the one that I'm looking for but mounting the drive I got the following error "New-PSDrive : The network resource type is not correct" I'm mounting with "New-PSDrive -Name T -PSProvider FileSystem -root $path -Persist" – Segolas Feb 29 '16 at 07:30
0

If it is a local path, you can try to access it via \\?\C:\very\long\path. This way you may not need to mount anything at all.

  • I can access it in Explorer. But I have to write a script in powershell to automate the removal of older files. Without the mount the script still fails with "PathTooLongException" which is in some way expected. With the mount it should not happen imho – Segolas Feb 26 '16 at 14:38
0

Use net use instead of New-PSDrive. You still need the UNC path to be shorter than 248 characters, though, so trim it with a regular expression:

$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

Overly long local paths can be handled the same way by using subst instead of net use.

Repeat the procedure if you the remaining path below the mapped drive is still longer than 248 characters.

And hit whoever is responsible for these insanely long paths with a cluestick. Until they pass out.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328