question

Ed7 avatar image
0 Votes"
Ed7 asked TomJebo-9587 edited

Handle exception with long paths



HI,

I am having issues with my script that cannot get the path from certain files/folders. When running this script I get the foollowing error.

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.


Others files/folders it does the job but I would like to know how to handle this and avoiding to give me this erro please.

After enabling the long file paths in Windows it still does not work.

If I add this **\\?** in the path variable, it failes saying that there are Illegal characters in path even if I add -LiteralPath.

This is what I have:


 #Message to user
      Write-Output "`nThis script will collect the FilePathName, Name, RelativePath and the Hash of the files/folders you have chosen."
                   "`nAt the end, it will tell you how long it took to run the script"   
      Read-Host -Prompt "`nPress ENTER to continue or CTRL+C to quit" 
            
      #Counting the time from the beginning
      $starttime = (Get-Date)
            
      #Variables
      #Can only be added 3 paths maximum. If added more than 3 it may change the relatrive path
      $root = "C:\mypath"
            
            
      #Variable for creating the CSV File
      $report = "mycsvfile.csv"
            
            
      #Process for generating the HASH
      $hasher = [System.Security.Cryptography.SHA256]::Create()
      $AllFiles = @() 
            
      "`n"#line space
            
      Write-Host "Generating the Hash from $root" 
            
            
            
      #Getting information from directories
      foreach ($file in get-childitem $root -recurse | Select-Object FullName, Directory, Name, PSIsContainer, Length)
      {
          $acl = get-acl $file.fullname | select-object owner,accesstostring,group
          $obj = new-object psObject
            
            
      #Generating HASH File
          if(!$file.PsIsContainer)
              {
              $relativePath = $file.FullName.Substring($root.Length)
              Write-Host "Debug $relativePath" -ForegroundColor Green
            
              $inputStream = New-Object IO.StreamReader $file.fullname
              $hashBytes = $hasher.ComputeHash($inputStream.BaseStream)
              $inputStream.Close()
            
              $builder = New-Object System.Text.StringBuilder
              $hashBytes | Foreach-Object { [void] $builder.Append($_.ToString("X2")) }
            
      #Add info into CSV FILE
              $obj | Add-Member -membertype noteproperty -name FilePathandName -Value $file.FullName
              $obj | Add-Member -membertype noteproperty -name Name -Value $file.Name
              $obj | Add-Member -MemberType noteproperty -Name RelativePath -Value $relativePath #-force
              $obj | Add-Member -MemberType noteproperty -Name Hash -Value $builder.ToString()
              #$obj | Add-Member -membertype noteproperty -name CreationTime -Value $file.CreationTime
              #$obj | Add-Member -MemberType noteproperty -Name LastAccessTime -Value $file.LastAccessTime
              #$obj | Add-Member -MemberType noteproperty -Name LastWriteTime -Value $file.LastWriteTime
            
                    
          #Variable to send info to CSV
          $AllFiles += $obj
          Clear-Variable relativePath
          }
      Remove-Variable obj
      }
            
      #$AllFiles += $obj
            
      #Generating CSV FILE
      $AllFiles |Export-Csv $report –NoTypeInformation
            
      "`n"
      Write-Host "$report File has been created "
            
      "`n"
      Write-Host "The script took:`n"
      $endTime = Get-Date
      New-TimeSpan -Start $startTime -End $endTime







Could someone help me on this please? I am really struggling with this.

Thank you


windows-serverwindows-server-powershelloffice-scripts-excel-dev
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Removing openspecs-questions as this does not pertain to the Open Specifications documents.

Best regards,
Tom Jebo
Sr Escalation Engineer
Microsoft Open Specifications Support

0 Votes 0 ·
cooldadtx avatar image
0 Votes"
cooldadtx answered MotoX80 commented

The file name length limit is dependent upon the OS and framework you're running on. 260 is the "old" limit which tends to indicate you're running an older OS or framework. Given your description it sounds like this might be a Powershell 5 script which runs on an older .NET Framework which could run into that issue.

The quick solution is to switch to Powershell 7+ which runs on .NET Core which shouldn't have this issue.

If that isn't an option then you do have to use the Unicode version which means prepending the file path with \\?\`. Thus a path of C:\temp` would become \\?\C:\Temp. This forces the call to use the right Win32 call that handles longer paths. But note that you can still get failures, depending upon how the code you're calling ultimately treats the path.

Yet another option is to force .NET to support the newer path by using a group policy starting with Windows 10 as discussed here. Unfortunately I've never tried to go that route with Powershell so I cannot say how it'll behave.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Because this question is a duplicate of https://docs.microsoft.com/en-us/answers/questions/670083/get-childitem-the-specified-path-file-name-or-both.html
I will close this. It doesn't make sense to work on the same question in two different threads.,


Kind regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.