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

327 views Asked by At

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

2

There are 2 answers

4
Ben Personick On BEST ANSWER

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

0
Andry On

This solution does not test it particularly for 255+ characters, but still tests in a long path specifically in a script inside cmd.exe.

Seems the stackoverflow incorrectly handles tabulation characters (and loses other characters like \x01 or \x04) in the copy-pasted code, so the below code might not work if you copy it directly by CTRL+C. Even the View Page Source does not show not printable characters. So to fix type ALT+0,4 after all eol= in all for loops.

Pros:

  • no need to check a path length

Cons:

  • a path must be an existed path
@echo off

setlocal

set "FILE_PATH=%~1"

if not defined FILE_PATH exit /b 1

for /F "eol= tokens=* delims=" %%i in ("%FILE_PATH%\.") do ^
for /F "eol= tokens=* delims=" %%j in ("%%~dpi.") do ( set "FILE_PATH=%%~fi" & set "FILE_DIR=%%~fj" )

if "%FILE_PATH:~0,4%" == "\\?\" set "FILE_PATH=%FILE_PATH:~4%"

echo "FILE_PATH=%FILE_PATH%"

rem for a path
if exist "\\?\%FILE_PATH%" if exist "%FILE_PATH%" ( echo.* a short path) else echo.a long path

rem for a file path
if exist "\\?\%FILE_PATH%" if not exist "\\?\%FILE_PATH%\*" if exist "%FILE_PATH%" ( echo.* a short file path) else echo.a long file path

rem for a directory path
if exist "\\?\%FILE_PATH%\*" if exist "%FILE_PATH%" ( echo.* a short directory path) else echo.a long directory path