Reputation: 3616
I have the below line in my powershell script in windows 7
$subFolderItems = Get-ChildItem $i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
The problem I get this error The specified path, file name, or both are too long
in some of the files.
I looked into it and found some suggestions to add \\?\
before the file path I tried as below and it's not working any advice?
$Base = '\\?\'
$subFolderItems = Get-ChildItem $Base$i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
Upvotes: 0
Views: 4856
Reputation: 61178
As you have commented, the path in $i.FullName
is in UNC format (\\server\share\restofpath
).
In that case, the prefix for long path should be \\?\UNC\
and the first two backslashes of the path itself need to be removed. In your case, it should be:
\\?\UNC\rackstation.mydom.com\main\sub.arch\mydom-customers\John_Marcus-123456
Using this prefix only works with the -LiteralPath
parameter of Get-ChildItem
.
Try
Get-ChildItem -LiteralPath ('\\?\UNC\' + $i.FullName.Substring(2))
For things like this, I always keep a small helper function handy:
function Add-LongPathPrefix([string] $Path) {
if ([string]::IsNullOrWhiteSpace($Path) -or $Path.StartsWith('\\?\')) { #'# nothing to do here
return $Path
}
if ($Path.StartsWith('\\')) {
# it's a UNC path like \\server\share\restofpath
return '\\?\UNC\' + $Path.Substring(2) #'# --> \\?\UNC\server\share\restofpath
}
else {
# it's a local path like X:\restofpath
return '\\?\' + $Path #'# --> \\?\X:\restofpath
}
}
Use it like this:
Get-ChildItem -LiteralPath (Add-LongPathPrefix $i.FullName)
P.S. For this you need to have Powershell 5.1 or higher
Hope that helps
Upvotes: 1