0

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

  • 1
    Very long path, _(> 256 characters)_, support is turned on by default in RoboCopy, because you 'd need the `/256` option to turn it off. Could you please provide some code and the proper paths so that we can try to reproduce the issue you say you're experiencing. – Compo Nov 10 '20 at 12:33
  • 1
    I was mistaken in believing robocopy was the origin of the error. I encountered the exception "PathTooLongException https://learn.microsoft.com/en-us/dotnet/api/system.io.pathtoolongexception – Derek Clark Nov 19 '20 at 12:36
  • Well your question is of no use to us or future readers then. I'd suggest that instead of your poor edit, and removal of bthe [[tag:robocopy]] tag, that you simply delete your question. In its current format, it appears that you're looking for off topic research material, or links, or are making an off topic code request. – Compo Nov 19 '20 at 12:41
  • Thank you for your feedback (and noted for future posting). I've revised my question in a way that should be more useful to others (steers attention to the selected answer which contains the code people may be seeking) – Derek Clark May 29 '21 at 09:07

1 Answers1

1

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

Ben Personick
  • 3,074
  • 1
  • 22
  • 29
  • 1
    This is an impressive answer, thank you. Identifying filepath+filename > 255 is goal, though for the output just identifying the corresponding folders is adequate (my intent is to manually review the list of especially long filepaths. Output to text file is ideal, thank you. – Derek Clark Nov 19 '20 at 12:08
  • 1
    I have 13 and need 15 :/ It lets me vote internally, though, so you get the algorithmic boost at some level -- just doesn't yet permit me to affect the votes shown yet. – Derek Clark Dec 01 '20 at 09:18
  • @derekclark ahh gotcha, I think it was 10 when I joined, or maybe I am missremembering, perhaps that was commenting that started at 10. Thanks for the upvote either way. Also it looks like you don't get comment upvotes added to you at this level of points perhaps as I upvoted that comment and your points didn't change, so I guess next time you ask/answer a Q it'll shake itself out. – Ben Personick Dec 01 '20 at 13:04
  • 1
    I finally have the reputation to validate your answer :) And I can also add that the code successfully identified about 500 filepaths, all of which could be resolved via a handful of moves (mounts) – Derek Clark May 29 '21 at 09:01