Script to find long path names

Welcome Forums General PowerShell Q&A Script to find long path names

This topic contains 12 replies, has 6 voices, and was last updated by

js
 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #110062

    Participant
    Points: 0
    Rank: Member

    Hello,

    I am trying to find a PowerShell script that will produce a list of all files\folders where the total path is more than 260 characters.

    I see the following but it doesn't seem to work.
      https://www.powershellmagazine.com/2012/07/24/jaap-brassers-favorite-powershell-tips-and-tricks/
     
    Dir d:\data\FolderName Get-ChildItem | Where-Object {$_.FullName.Length -gt 260}
    I also tried Get-ChildItem | Where-Object {$_.FullName.Length -gt 260}  from https://stackoverflow.com/questions/12697259/how-do-i-find-files-with-a-path-length-greater-than-260-characters-in-windows but that does work.

    Can someone let me know what I am missing?

    thanks,

    Roger

     

  • #110065

    Participant
    Points: 12
    Rank: Member
  • #110102

    Participant
    Points: 331
    Helping Hand
    Rank: Contributor

    With recent version of Windows, you can set a registry change to deal with long file names.

    https://blogs.msdn.microsoft.com/jeremykuhne/2016/07/30/net-4-6-2-and-long-paths-on-windows-10

  • #110110
    js

    Participant
    Points: 326
    Helping Hand
    Rank: Contributor

    This?

    Get-ChildItem -Recurse -Force -ErrorAction SilentlyContinue | 
      Where-Object {$_.FullName.Length -gt 260}
    • #110165

      Participant
      Points: 0
      Rank: Member

      Hi Js, Sorry that doesn't appear to work.

  • #110167

    Participant
    Points: 5
    Rank: Member

    Roger,

    When you say it doesn't work, what do you mean?  Is it giving you an error?  If so, can you please post.  If it is providing incorrect results can you elaborate?

     

    • #110179

      Participant
      Points: 0
      Rank: Member

      Using that script provides no results.  It doesn't error out but nothing happens.

  • #110182
    js

    Participant
    Points: 326
    Helping Hand
    Rank: Contributor

    If you want to search from the top folder on the c drive, c:\, it would be (or cd to it first):

    Get-ChildItem C:\ -Recurse -Force -ErrorAction SilentlyContinue | 
      Where-Object {$_.FullName.Length -gt 260}
    • #110485

      Participant
      Points: 0
      Rank: Member

      Sorry I am really a beginner.  I modified your command  to try to get a list of files in a directory that exceed 260 characters as follows after selecting: cd d:\
      Get-ChildItem D:\Data\”Communications Team” -Recurse -Force -ErrorAction SilentlyContinue |  Where-Object {$_.FullName.Length -gt 260}
      After a few seconds the PS D:\> prompt returns but doesn't do anything or produce any results.  What am I missing?

       

    • #110582
      js

      Participant
      Points: 326
      Helping Hand
      Rank: Contributor

      It works for me. What if you take out the "-erroraction silentlycontinue" option? Is there an error message?

          Directory: C:\WINDOWS\SoftwareDistribution\Download\ae84ea9d8e2d5b57697fdac2a81468b1\amd64_Microsoft-Windows-NetFx3-OnDemand-Package~~AMD64~~10.0.17134.1\amd64_netfx-aspnet_installsq
          lstatetemp_b03f5f7f11d50a3a_10.0.17134.1_none_ff11cd80956b65aa
      
      
      Mode                LastWriteTime         Length Name
      ----                -------------         ------ ----
      -a----       10/29/2017   9:09 PM          53903 installsqlstatetemplate.sql
      
      
          Directory: C:\WINDOWS\SoftwareDistribution\Download\ae84ea9d8e2d5b57697fdac2a81468b1\amd64_Microsoft-Windows-NetFx3-OnDemand-Package~~AMD64~~10.0.17134.1\amd64_netfx-aspnet_personali
          zation_sql_b03f5f7f11d50a3a_10.0.17134.1_none_bdf33e156afb2609
      
      
      Mode                LastWriteTime         Length Name
      ----                -------------         ------ ----
      -a----       10/29/2017   9:12 PM           7489 uninstallpersonalization.sql
      
      
          Directory: C:\WINDOWS\SoftwareDistribution\Download\ae84ea9d8e2d5b57697fdac2a81468b1\amd64_Microsoft-Windows-NetFx3-OnDemand-Package~~AMD64~~10.0.17134.1\amd64_netfx-aspnet_uninstall
          persistsql_b03f5f7f11d50a3a_10.0.17134.1_none_53b314e56d2c48b4
      
      
      Mode                LastWriteTime         Length Name
      ----                -------------         ------ ----
      -a----       10/29/2017   9:18 PM          10195 uninstallpersistsqlstate.sql
    • #110875

      Participant
      Points: 0
      Rank: Member

      Hi JS,

      Unfortunately, I get the following error.

      PS D:\data> Get-ChildItem ”Communications Team” -Recurse |  Where-Object {$_.FullName.Length -gt 260}
      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 line:1 char:1
      + Get-ChildItem ”Communications Team” -Recurse |  Where-Object {$_.FullName.Length ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : ReadError: (D:\data\Communi...al Photos Files:String) [Get-ChildItem], PathTooLongExcept
      ion
      + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

  • #110498

    Participant
    Points: 297
    Helping Hand
    Rank: Contributor

    Get-ChildItem isn't coded to allow for longer paths, from what I recall. So it won't find anything.

    What you can do is grab the NTFSSecurity module (Import-Module NTFSSecurity), which contains a Get-ChildItem2 cmdlet. It works the same as Get-ChildItem, but utilises a third party DLL to handle longer paths as well.

  • #111214
    js

    Participant
    Points: 326
    Helping Hand
    Rank: Contributor

    Have you tried PS 6? There's something about long pathnames that might get backported to PS 5. https://github.com/PowerShell/PowerShell/pull/3960

    There might be a long pathname syntax?

    "When calling Windows native API to determine if an item exists, ensure the path is prepended with \\?\ to allow for paths > 260 characters."

    EDIT:

    Try this:

    Get-ChildItem -literalpath \\?\C:\ -Recurse | 
      Where-Object {$_.FullName.Length -gt 260}

    reference: https://stackoverflow.com/questions/46308030/handling-path-too-long-exception-with-new-psdrive/46309524

    Btw what version of powershell do you have? I don't have any problems in 5.1.16299.611.

The topic ‘Script to find long path names’ is closed to new replies.