batch-filewindows-7filepathpathtoolongexception

Windows file PathTooLongException: Batch file to Identify filepaths longer than 255


Problem I have some filepaths exceeding length=255 that, evidently, cannot be handled (and appear to be disrupting entire operations) during file sync operations on my Windows 7 machine. The exception encountered is PathTooLongException

Desired Solution: I'd like to create a batch file that identifies filepaths that exceed 255, and outputs them to a .txt file (for diagnostic review)

UPDATE The accepted solution below works perfectly.

this is my first post here; please be kind


Solution

  • Do you want just paths (directory paths over 255 chars) or combo filenamr and folder path

    Just folders:

    @(Setlocal enabledelayedexpansion
      Echo off
      Set "_Root=D:\"
      Set "_ResultFile=D:\PathsOver255Chars.txt"
    )
    
    CALL :Main
    
    ( Endlocal
      Exit /B
    )
    
    :Main
      For /F "Tokens=*" %%_ IN ('
        Dir /B /S /AD "%_Root%*"
      ') DO (
        SET "_CheckLen=%%_"
        IF /I !_CheckLen! NEQ !_CheckLen:~-255! (
          ECHO=%%_>>"%_ResultFile%"
        )
      )
    GOTO :EOF
    
    

    Just files:

    @(Setlocal enabledelayedexpansion
      Echo off
      Set "_Root=D:\"
      Set "_ResultFile=D:\PathsOver255Chars.txt"
    )
    
    CALL :Main
    
    ( Endlocal
      Exit /B
    )
    
    :Main
      For /F "Tokens=*" %%_ IN ('
        Dir /B /S /A-D-S-H "%_Root%*"
      ') DO (
        SET "_CheckLen=%%_"
        IF /I !_CheckLen! NEQ !_CheckLen:~-255! (
          ECHO=%%_>>"%_ResultFile%"
        )
      )
    GOTO :EOF
    
    

    Folders and files

    @(Setlocal enabledelayedexpansion
      Echo off
      Set "_Root=D:\"
      Set "_ResultFile=D:\PathsOver255Chars.txt"
    )
    
    CALL :Main
    
    ( Endlocal
      Exit /B
    )
    
    :Main
      For /F "Tokens=*" %%_ IN ('
        Dir /B /S  "%_Root%*"
      ') DO (
        SET "_CheckLen=%%_"
        IF /I !_CheckLen! NEQ !_CheckLen:~-255! (
          ECHO=%%_>>"%_ResultFile%"
        )
      )
    GOTO :EOF