Worked example - Find the longest file path in your codebase

·

1 min read

I recently wanted to know the length of the longest file path within my codebase. Someone was struggling to pull the code to their local machine, and I found that Windows has a 256-character limit for a file path. Well, sort of. It turns out you can edit the registry to remove this restriction in Windows 10. There are instructions at How-To Geek.

If you want to check how long the file paths are, how would you go about it? This is what I did:

  1. Use the dir command to get a list of all file paths. The documentation lists various options, but the relevant ones for me were /S and /b so that subfolders were included and the output only contains the file paths, without additional information.
    This command should be run at your repository root. You can redirect the output into a text file to make it easier to work with.

     cd "C:\src\ProjectName"
     dir /S /b > "C:\MyFolder\filepaths.txt"
    

    This creates a file with contents along these lines:

     C:\src\ProjectName\File1.cs
     C:\src\ProjectName\Folder\File2.cs
     C:\src\ProjectName\Folder\Subfolder\File3.cs
     C:\src\ProjectName\Folder\Subfolder\File4.cs
    
  2. Copy the contents of the text file and paste everything into excel.

  3. Use the LEN function to get the length of each row.

  4. Use excel to sort the rows so you can easily find the longest.