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,354 times
Add to favorites
Storage
8/28/2013
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Script is not working
    1 Posts | Last post April 11, 2014
    • I am getting the following Error messages. Can you please help me to fix it. 
      
      
      The term 'Get-QADComputer' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
       spelling of the name, or if a path was included, verify that the path is correct and try again.
      At C:\Users\smehboob.PK\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:25 char:23
      + $DCs = Get-QADComputer <<<<  -ComputerRole DomainController
          + CategoryInfo          : ObjectNotFound: (Get-QADComputer:String) [], CommandNotFoundException
          + FullyQualifiedErrorId : CommandNotFoundException
       
      The term 'Get-QADcomputer' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
       spelling of the name, or if a path was included, verify that the path is correct and try again.
      At C:\Users\smehboob.PK\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:28 char:29
      + $Computers = Get-QADcomputer <<<<  -Service $dc.dnshostname -ip lastlogontimestamp
          + CategoryInfo          : ObjectNotFound: (Get-QADcomputer:String) [], CommandNotFoundException
          + FullyQualifiedErrorId : CommandNotFoundException
       
      Exception calling "ContainsKey" with "1" argument(s): "Key cannot be null.
      Parameter name: key"
      At C:\Users\smehboob.PK\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:40 char:27
      + If ($LastLogon.ContainsKey <<<< ($ComputerName))
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : DotNetMethodException
       
      
  • Spaces in path
    1 Posts | Last post December 10, 2013
    • Having the same issue as U333 but for some reason his fix didn't work for me?
      
      So I did this:
      
      $CurrentPath = ($_).substring($_.indexof(":") - 1)
  • No output
    2 Posts | Last post December 10, 2013
    • Hey Jaap,
      
      I'm getting zero output too!
      
      execution policy changed, tried dot sourcing, still nothing. 08R2 Posh v2.0
      
      Any help appreciated.
      
      Nick
    • Working now, can't see why but also don't care :)
      
      On another note I had the same issue as U333.
  • Problem with Large Scale Scans
    2 Posts | Last post October 10, 2013
    • It appears as though using this method has 1 flaw. If doing a large scale scan 1TB of data or more It will consume all system memory. I personally have 8GB of RAM and it caused powershell to consume just over 8,000,000 K i had to kill the script in order to use my PC. Is there any way to limit it's memory usage to a set limit?
    • Interesting case, I have not tried to run the script on a large file structure but I can think of a method to work around this. An updated version of the script will be uploaded will be uploaded here in which the excessive memory usage should no longer be a problem. I will post an update here as soon as it is ready.
  • No results
    12 Posts | Last post August 28, 2013
    • Hi,
      
      I tried to use this function a couple of times but just like "Rami Ha" i just don't get any results.
      
      If i try:
      
      Get-LongPathNames -FolderPath 'C:\Program Files'
      
      just nothing happens but i don't get an error.
      I also tried to export it to a .csv but that doesn't work either.
      The .csv File is created but it is just empty.
      I am using Windows 7 with Powershell 3.0 and robocopy is definitely installed.
      
      Do you have any suggestions what the problem could be?
      
      Thanks in advance!
    • Could you try running the following command:
      .\Get-LongPathName.ps1
      Get-LongPathNames -FolderPath 'C:\Program Files' -MaxDepth 70
      
      Does that generate any output for you?
    • Sorry something went wrong there, what I meant was can you execute the following two commands:
      . .\Get-LongPathName.ps1
      Get-LongPathNames -FolderPath 'C:\Program Files' -MaxDepth 70
    • No I don't get any output from that.
    • Okay, so let's verify if the function is loaded correctly. What kind of Output do you get if you run either of these two commands:
      Get-Help Get-LongPathNames
      Get-Command Get-LongPathNames -Syntax
      
      Otherwise can you try to run the command against a different folder, for example the system32 folder by running the following command:
      Get-LongPathNames c:\windows\system32 -MaxDepth 20
      
    • PS W:\> Get-Help Get-LongPathNames
      NAME
          Get-LongPathNames
      ÜBERSICHT
          Function to get file and folder names with long paths.
      SYNTAX
          Get-LongPathNames [-FolderPath] <String[]> [-MaxDepth <Int16>] [<CommonParameters>]
      BESCHREIBUNG
          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.
      VERWANDTE LINKS
          http://www.jaapbrasser.com
      HINWEISE
          Zum Aufrufen der Beispiele geben Sie Folgendes ein: "get-help Get-LongPathNames -examples".
          Weitere Informationen erhalten Sie mit folgendem Befehl: "get-help Get-LongPathNames -detailed".
          Technische Informationen erhalten Sie mit folgendem Befehl: "get-help Get-LongPathNames -full".
          Geben Sie zum Abrufen der Onlinehilfe Folgendes ein: "get-help Get-LongPathNames -online"
      
      
      PS W:\> Get-Command Get-LongPathNames -Syntax
      Get-LongPathNames [-FolderPath] <string[]> [-MaxDepth <int16>] [<CommonParameters>]
      
      I tried different folders but i can't get a result for any of them.
      
    • Okay, this might be a long shot, but I see you are using German localization options. Can you provide me a fraction of output from robocopy. If the output of robocopy is also in German or has different padding/spacing then the script might indeed fail.
      
      Could you perhaps run a robocopy command on a small folder structure, one file one folder and post the output here? I do not have access to a German version of Windows, so I cannot test this myself.
    • Thas is correct the language of the robocopy output seems to change accordign to the system language so i tried to switch to english and got an english output of robocopy but the function didn't work either.
      
      This is the output of the german robocopy:
      
      PS W:\> Robocopy w:\robo1 w:\robo2 -mir -copy:dt -r:3 -w:3 -eta
      
      -------------------------------------------------------------------------------
         ROBOCOPY     ::     Robustes Dateikopieren für Windows
      -------------------------------------------------------------------------------
      
        Gestartet: Wed Aug 28 07:15:09 2013
      
         Quelle : w:\robo1\
           Ziel : w:\robo2\
      
          Dateien : *.*
      
        Optionen: *.* /S /E /COPY:DT /PURGE /MIR /ETA /R:3 /W:3
      
      ------------------------------------------------------------------------------
      
                                 1    w:\robo1\
      100%        Neue Datei                 0        robo5.txt
               Neues Verz.       1    w:\robo1\robo3\
      100%        Neue Datei                 0        robo4.txt
      
      ------------------------------------------------------------------------------
      
                 Insgesamt   KopiertÜbersprungenKeine Übereinstimmung    FEHLER    Extras
      Verzeich.:         2         1         1         0         0         0
        Dateien:         2         2         0         0         0         0
          Bytes:         0         0         0         0         0         0
         Zeiten:   0:00:00   0:00:00                       0:00:00   0:00:00
      
        Beendet: Wed Aug 28 07:15:09 2013
    • Okay Julian, based on your input I have created a version of the script that should be language agnostic. Since there is no proper method of sharing this I will post the truncated code here. Could you give this a spin and let me know how this functions with the german version of robocopy?
      
      Function Get-LongPathNames {
      param(
              $FolderPath,
      		$MaxDepth=248
      )
          process {
              foreach ($Path in $FolderPath) {
                  $RoboOutput = robocopy.exe $Path c:\doesnotexist /l /e /b /np /fp /njh /njs /r:0 /w:0
       
                  # Build Array for files. 26 is added to $MaxDepth the text string generated by Robocopy
                  $RoboOutput | Where-Object {$_} |
                  ForEach-Object {
                      $CurrentPath = ($_ -split '\s')[-1]
                      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'
                                     }
                          }
                      }
                  }
              }
          }
      }
    • YES!!! It actually works but only if I start the powershell as administrator.
      
      Thank you so much for all your help I really appreciate it!!!
    • Great, nice to hear this fixed your issue. I will upload the updated code to TechNet as this code is actually also more efficient for the English language since it is not longer necessary to use Select-String to parse through the output.
      
      Thanks for bringing this to my attention Julian, this should fix issues with this script for other regions as well!
    • The updated script has been uploaded, feel free to give the final version a try.
  • Issues using this script on Windows XP. Any suggestions?
    2 Posts | Last post May 01, 2013
    • Hi.
      
      I tried using this script on Windows XP. However, I received an error.
      
      The error I receieved was:
      
      The term 'robocopy.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of th
      e name, or if a path was included, verify that the path is correct and try again.
      At C:\Documents and Settings\Eugeniusz Budrewicz\Desktop\scripts\GetExcelSHInfo_v0.1.ps1:88 char:40
      +              $RoboOutput = robocopy.exe <<<<  $Path c:\doesnotexist /l /s /b /np /fp /njh /njs /ndl /W:0 /R:0 /256 /IF *.xlsx, *.xls,
       *.xlsm, *.xlsxm 
          + CategoryInfo          : ObjectNotFound: (robocopy.exe:String) [], CommandNotFoundException
          + FullyQualifiedErrorId : CommandNotFoundException
      
      Any suggestions on how to fix this? I read that this was tested on Windows XP and actually works. 
      
      Thanks.
    • That is correct, it does work for Windows XP, but the requirement is that robocopy is installed on the system. If you would like to download robocopy it is freely available on the Microsoft website as part of the Windows Server 2003 Resource Kit:
      http://www.microsoft.com/en-us/download/details.aspx?id=17657
      
      I will update the description of the script to avoid future confusion with XP Compatibility.
  • Nice script, but what about logging?
    4 Posts | Last post May 01, 2013
    • Thank you for this nice script. I'd like to use it for finding long path files on a file server.
      Unfortunately the output that could be logged to identify the files is too short to read. Se example below.
      Do you have an idea how to get the full or at least longer path output?
      
      Type                                       PathLength FullPath                 
      ----                                       ---------- --------                 
      File                                              340 c:\temp\Test_Long_Path...
      File                                              340 c:\temp\Test_Long_Path...
      File                                              340 c:\temp\Test_Long_Path...
      File                                              340 c:\temp\Test_Long_Path...
      File                                              340 c:\temp\Test_Long_Path...
    • For logging I would probably export to csv, as that would give you the complete filenames. So to do that you could run the command as such:
      
      
      Get-LongPathNames -FolderPath C:\Ernie1 -MaxDepth 248 | Export-Csv -Path C:\LongPaths.csv
      
      If you would like to have the output in the console you would probably be forced to use Format-List to get all the information correctly displayed. So try this for better console output:
      
      Get-LongPathNames -FolderPath C:\Ernie1 -MaxDepth 248 | Format-List
    • Get-LongPathNames -FolderPath C:\Ernie1 -MaxDepth 248 | Export-Csv -Path C:\LongPaths.csv
      
      That's absolutely great! Exaclty what's needed!
      Thank you a lot!
    • No problem, glad to hear it helped
  • Number of Retries
    2 Posts | Last post April 15, 2013
    • I ran your script and it hung up on me. I ran the robocopy command in CMD to figure out what was wrong. Turns out one of the sub-directories I didn't have permissions to, this was a test folder I forgot I had. So robocopy produced this error "YYYY/MM/DD HH:MM:SS ERROR 5 (0x00000005) Scanning Source Directory C:\test\nopermission." So you might want to add the "/R:n" switch and add another line to catch those errors. If that is not added, a person trying to run your script could be running it for 347 days before it completes. 
    • Good call, I added the /r:0 /w:0 switches to the robocopy command so this behaviour does not occur anymore. Thanks for reporting the bug!
  • A little help please
    2 Posts | Last post October 05, 2012
    • I can get the function to work (as in not error out), but I get nothing. No results, no output. I'm new to PowerShell. Thanks in advance.
    • Depends what kind of parameters you are using to run it. I noticed there were some inconsistencies when using the -FolderPath parameter. As a test can you try to run it with the following parameters:
      
      Get-LongPathNames -FolderPath 'C:\Program Files'  
11 - 19 of 19 Items