Function to get file and folder names with long paths

This function requires Robocopy to be installed. Robocopy is used to recursively search through a folder structure to find file or folder names that have more than a certain number of characters. The function returns an object with three properties: FullPath,Type and FullPath.

 
 
 
 
 
4.2 Star
(9)
13,749 times
Add to favorites
Storage
8/28/2013
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Where does it output to?
    1 Posts | Last post July 06, 2018
    • Where does the output for the function go? Is it displayed on the screen if I use the same semantics as the example use cases? Do I need to somehow handle or process the Object that is output from the function?
  • How do I start?
    1 Posts | Last post September 19, 2017
    • I'm completely new to scripts.  How do I begin to start?  I am running Windows 10, so I know that Robocopy is already installed on the machine by default (based on my research).  Can someone give me insight on how to start running this script?
  • Filter by age
    3 Posts | Last post November 16, 2016
    • Hi Jaap, I am currently working on a script that searches a large datastore for all files older than 7 years. I am getting errors with long path name files. I am currently using your script to gather all these files into a csv. I would like to further filter this search function you have made to filter by files with the /MAXLAD:2555 switch. Am I correct in assuming that the switch would be added here - "$RoboOutput = robocopy.exe $Path c:\doesnotexist /l /e /b /np /fp /njh /njs /MAXLAD:2555 "
      
      Or do you suggest making a variable control for this and if so how would you structure it, I am thinking of a few different ways but I am a rookie at best when it comes to powershell scripting!
      
      Thanks for this great script by the way!
    • My assumption is correct, since I am running this function within a much larger script I inserted my stored variable in this function to call upon instead of having to specify each time the days.
      
      This works fine, overall it took about 20mins to scan a network share of 2TB and filtered out and found 73004 files over 7 years with long path names that my 7 year gci is not able to find because of this issue. I was able to append these results to the relevant csv files.
      
      Thank you again Jaap for this brilliant function script that is easy to understand and modify where required.
      
      
    • Hello Daniel,
      
      Thanks for checking back with a solution after asking a question. This will be helpful to others that have run into similar questions like yours. Robocopy is indeed a great tool for this purpose, another alternative would be using dir.exe and regular expressions but that is a more complicated approach which would be slower than robocopy as well. All-in-all robocopy is a great tool that can be leveraged really great for scenarios like yours.
      
      
      Regards,
      
      Jaap Brasser
  • No Output
    3 Posts | Last post March 11, 2016
    • I have tried different way to run it but i just doesn't get any output:
      . .\Get-longPathName.ps1 Get-LongPathName -FolderPath D:\DATA -MaxDepth 256
      
      What am i doing wrong?
    • Just got it... you need 2 lines to run it:
      
      
      . .\Get-LongPathName.ps1
      Get-LongPathName -FolderPath c:\scripts -MaxDepth 10
    • That is correct, the new line is required, alternatively this will also work:
      
      . .\Get-longPathName.ps1 ; Get-LongPathName -FolderPath D:\DATA -MaxDepth 256
  • Dot Net Class
    3 Posts | Last post November 17, 2015
    • Hi Jaap,
      
      I found a dotnet class made by Microsoft for if I understood exceeded the limit of 250 characters in the path of a folder.
      I'm not a specialist like the dot net powershell.
      I would use this class if it works in powershell
      I do not know how to load this class and how to use.
      Here are the links:
      http://bcl.codeplex.com/wikipage?title=Long%20Path%20Samples&referringTitle=Long%20Path
      // http://bcl.codeplex.com/workitem/7589
      
      Hoping that these informations help you and also me.
      
      Have a nice week-end.
      
    • I am aware of this class, and while it is useful I do not think it has a place in this particular script. The goal of this script was to show long paths without having to install any additional resources and this .Net module would have to be downloaded and then loaded in to PowerShell.
      
      If you want to learn more about alternatives to the standard Windows APIs for managing the file system I would suggest taking how this is implemented in the File System Security PowerShell Module: https://gallery.technet.microsoft.com/scriptcenter/1abd77a5-9c0b-4a2b-acef-90dbb2b84e85
      
      In this module Raimund Andrée has implemented AlphaFS for dealing with long path names.
    • Thanks for your answer.
  • Not Working 2012 R2
    3 Posts | Last post August 12, 2015
    • Trying to use this - verified that robocopy in on server.  no errors, nothing just doesn't output anything.  Figured maybe I didn't have any long file paths but it finishes way to fast.  Perhaps a problem with robocopy that ships with 2012?
      
      .\Get-LongPathName.ps1 -FolderPath D:\ -MaxDepth 4 | Export-Csv -Path c:\longpath.csv
    • Seems like I am not the only one that has issues getting this to work.  Get idea - but would be nice to get some feedback from the author on out of box issues and how to resolve.  Nothing below works.  Guessing I am not doing it right.
      
      Thanks
      John
    • Indeed, that is correct the script does not work with the syntax you are using. This is because the script contains a function in the script, which needs to be dot sourced first. Secondly your command would not work because the minimum path length is actually 10, so try the following code and let me know how that works for you:
      
      . .\Get-LongPathName.ps1
      Get-LongPathName -FolderPath c:\scripts -MaxDepth 10
  • returns PathLength, Type and FullPath
    1 Posts | Last post January 08, 2015
    • In your description, you state: "The function returns an object with three properties: FullPath,Type and FullPath.
      
      I think you meant to say: PathLength, Type and FullPath
  • Paths with spaces do not work
    4 Posts | Last post October 07, 2014
    • Hi Jaap,
      i had a problem by using your function with paths containing spaces. So i made a small correction to your script by using REGEX-Splitting for the robocopy output. Now it works as expected:
      
      Function Get-LongPathName {
      param(
          [CmdletBinding()]
          [Parameter(
          Position=0, 
          Mandatory=$true, 
          ValueFromPipeline=$true,
          ValueFromPipelineByPropertyName=$true)]
          [string[]]
              $FolderPath,
          [ValidateRange(10,248)]
      	[int16]
      		$MaxDepth=248
      )
      
          begin {
              if (!(Test-Path -Path $(Join-Path $env:SystemRoot 'System32\robocopy.exe'))) {
                  write-warning "Robocopy not found, please install robocopy"
                  return
              }
          }
      
          process {
              $regex = [Regex] "\t"
              foreach ($Path in $FolderPath) {
                  $RoboOutput = robocopy.exe $Path c:\doesnotexist /l /e /b /np /fp /njh /njs /r:0 /w:0
       
                  $RoboOutput | Where-Object {$_} |
                  ForEach-Object {
                      $CurrentPath =  $regex.Split($_)[2]
                      if ($CurrentPath.Length -gt $MaxDepth) {
                          New-Object -TypeName PSCustomObject -Property @{
                              FullPath = $CurrentPath
                              PathLength = $CurrentPath.Length
                              Type = if ($CurrentPath.SubString($CurrentPath.Length-1,1) -eq '\') {
                                         'Folder'
                                     } else {
                                         'File'
                                     }
                          }
                      }
                  }
              }
          }
      }
      
      Regards
      Uwe
    • This script would appear to only show folders not files over 260 char limit.
    • Hi Jaap/U333,
      This was very useful, thanks so much for original script. I tried U333's update above and it only gets folders as previously stated. Can someone recommend what update will be needed to get file names with spaces as well?
    • no worries, I was able to play around with the code long enough to find a very simple solution i.e. just change the below line Jaap's original script like so... 
      
      --Original--
      $CurrentPath = ($_ -split '\s')[-1]
      
      --Solution for spaces in folders AND file names--
      $CurrentPath = ($_ -split '\t')[-1]
      
      (yep, that simple, switch the splitter from \s to \t)
  • not working for me :(
    2 Posts | Last post September 30, 2014
    • hi jaap
      i was trying to use this script and initially didn't work. but suddenly it was working and i couldn't save the script. if i download your script and run, it will not work. as U333 say, there might be some space issue on script or some thing else. even copy to notepad, save as ps1 files didn't work for me. basically when i execute script, it will run without any issues, but it's believe after that this "Get-LongPathName"  should work with parameters. so it doesn't work. can know what is the easy method use this script. i am not script guy and can't find the bug in this script 
    • oh it's silly mistake . it's required Space(^) to load. 
       .^.\Get-LongPathName.ps1
  • Can i use this script against shared folders?
    1 Posts | Last post May 14, 2014
    • Hi Jaap,
      
      Thanks for your script.
      
      I had used your script against local folders, it is working fine. When i use against the shared drive/folders there is no output getting printed on the console even though there are some file paths having lengths too long. Any help is appreciated.
1 - 10 of 19 Items