none
How to list all files in a directory and subdirectories RRS feed

  • Question

  • Hello.

    How to list all files in a directory and subdirectories and exports them to Excel? (without using DIR).

    Within Excel, each line must have the path\filename.

    Within Excel I'll use the LEN formula to determine which path exceeds 256 characters, because of problems copying files that have the path too long.

    Thanks.

    Tuesday, May 4, 2010 4:57 PM

Answers

  • This command prints the path\filenames that exceeds 256 characters?

    Yes.

    get-childitem 'c:\whatever' -recurse -force | where-object { $_.FullName.Length -ge 256 } | select-object FullName | format-list

    In this command, get-childitem -recurse retrieves items recursively, where-object filters for items whose full names have a length greater than or equal to 256 characters, and select-object retrieves the FullName property for each item. Finally, we output to format-list to get a list. You can also use export-csv (as Sean did) to output this list to a CSV file.

    HTH,

    Bill

    • Marked as answer by IamMred Monday, May 17, 2010 6:53 AM
    Wednesday, May 5, 2010 2:28 PM
    Moderator
  • Also, you may be interested that file path length limitations come from .NET, not PowerShell:

    https://connect.microsoft.com/PowerShell/feedback/details/240949/

    The following .NET blog entry has more about this problem:

    http://blogs.msdn.com/bclteam/archive/2007/02/13/long-paths-in-net-part-1-of-3-kim-hamilton.aspx

    A workaround to the problem might be to create a junction to a deeply-embedded path and then use the junction. For example:

    linkd c:\stuff 'c:\documents and settings\user2\local settings\temporary internet files\content.ie5'

    After this command, the directory 'c:\stuff' is really a reference to the longer path. Use 'linkd c:\stuff /d' when you're finished to delete the link. The linkd.exe utility is available in the Server 2003 Resource Kit tools.

    HTH,

    Bill

    • Marked as answer by IamMred Monday, May 17, 2010 6:53 AM
    Wednesday, May 5, 2010 6:54 PM
    Moderator
  • Hi,

    Try my 'junction' workaround, posted above.

    Bill

    • Marked as answer by IamMred Monday, May 17, 2010 6:53 AM
    Wednesday, May 5, 2010 7:50 PM
    Moderator

All replies

  • PowerShell:

    get-childitem 'c:\whatever' -recurse -force | where-object { $_.FullName.Length -ge 256 } | select-object FullName | format-list

    Bill
    Tuesday, May 4, 2010 5:09 PM
    Moderator
  • Hello Bill!

    This command prints the path\filenames that exceeds 256 characters? I need the completepath\filename . Example: E:\folder1\folder2\folder3\file.txt

    Thanks.

    Tuesday, May 4, 2010 5:58 PM
  • Here's a "QuickandDirty" solution :)

    get-childitem C:\Foldername -recurse | select-object DirectoryName,Name | where { $_.DirectoryName -ne $NULL } | Export-CSV C:\Filelist.csv

    Now mind you, this will give two columns in Excel, Foldername and filename but it will list ONLY files and WILL show the full foldername for each file.  It could be prettier but hey, this was ad-hoc ;)

    Sean


    Powershell. It's so Easy and it's FREE! Dive in and use it now, It'll take no time. :) http://www.energizedtech.com http://www.itprotoronto.ca
    • Marked as answer by bbastos Tuesday, May 4, 2010 7:17 PM
    • Unmarked as answer by bbastos Wednesday, May 5, 2010 12:26 PM
    Tuesday, May 4, 2010 6:32 PM
    Moderator
  • Thanks Sean and Bill.

    The Sean's command solved my problem.

    Thanks for the help.

    Tuesday, May 4, 2010 7:14 PM
  • Hello Sean,

    After some testing, I discovered that the script does not print the paths \ files that have names that exceed 256 characters. Is there a way to show what are the files that have exceeded the 256 characters?

    Thanks.

    Wednesday, May 5, 2010 12:32 PM
  • This command prints the path\filenames that exceeds 256 characters?

    Yes.

    get-childitem 'c:\whatever' -recurse -force | where-object { $_.FullName.Length -ge 256 } | select-object FullName | format-list

    In this command, get-childitem -recurse retrieves items recursively, where-object filters for items whose full names have a length greater than or equal to 256 characters, and select-object retrieves the FullName property for each item. Finally, we output to format-list to get a list. You can also use export-csv (as Sean did) to output this list to a CSV file.

    HTH,

    Bill

    • Marked as answer by IamMred Monday, May 17, 2010 6:53 AM
    Wednesday, May 5, 2010 2:28 PM
    Moderator
  • Hello Bill,

    The following error appear when I ran the command:

    Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 26
    0 characters, and the directory name must be less than 248 characters.

     

    Wednesday, May 5, 2010 5:05 PM
  • Hi,

    Need more of the error message.

    Bill

    Wednesday, May 5, 2010 6:38 PM
    Moderator
  • Also, you may be interested that file path length limitations come from .NET, not PowerShell:

    https://connect.microsoft.com/PowerShell/feedback/details/240949/

    The following .NET blog entry has more about this problem:

    http://blogs.msdn.com/bclteam/archive/2007/02/13/long-paths-in-net-part-1-of-3-kim-hamilton.aspx

    A workaround to the problem might be to create a junction to a deeply-embedded path and then use the junction. For example:

    linkd c:\stuff 'c:\documents and settings\user2\local settings\temporary internet files\content.ie5'

    After this command, the directory 'c:\stuff' is really a reference to the longer path. Use 'linkd c:\stuff /d' when you're finished to delete the link. The linkd.exe utility is available in the Server 2003 Resource Kit tools.

    HTH,

    Bill

    • Marked as answer by IamMred Monday, May 17, 2010 6:53 AM
    Wednesday, May 5, 2010 6:54 PM
    Moderator
  • Hello Bill,

    The rest of error message display the path that exceed 256 characters.

    I really need to print the filepaths that exceeds 256 characters, because I need to rename these paths and filenames before copying them (file server migration).

    Thanks.

    Wednesday, May 5, 2010 7:28 PM
  • Hi,

    Try my 'junction' workaround, posted above.

    Bill

    • Marked as answer by IamMred Monday, May 17, 2010 6:53 AM
    Wednesday, May 5, 2010 7:50 PM
    Moderator
  • This worked for me

    dir '\Users\<type your username here>\downloads\temp' -file -recurse | Select Name,@{Name="NameLength";Expression={$_.name.length}} | Sort NameLength -Descending

    Sunday, May 28, 2017 9:43 AM