For Each File In Folder.Files
works fine as long as the lengths of the file names (incl. Path) are within the limit of Windows, which is 259 characters. But if a file name (incl. Path) is longer, then it doesn't work.
Here's an example:
ShortPath = "C:\TEST"
LongPath = "C:\TESTLONG"
Set FSO = CreateObject("Scripting.FileSystemObject")
Call FSO.CreateFolder(ShortPath)
Call FSO.CreateTextFile(ShortPath & "\" & String(240, "A"), True)
Call FSO.CreateTextFile(ShortPath & "\" & String(250, "B"), True)
Call FSO.MoveFolder(ShortPath, LongPath)
Set Folder = FSO.GetFolder(LongPath)
For Each File In Folder.Files
Msgbox("File: " & File.Name & vbCrLf & "Length: " & Len(LongPath & "\" & File.Name))
Next
By renaming the folder "C:\TEST" to "C:\TESTLONG", the length of the file name "AAA..." (incl. path) is 252 (12+240) characters, which is still less than 260 characters. Therefore this file is listed.
But the length of the file name "BBB..." (incl. path) is now 262 (12+250) characters, which is more than 259 characters, which means that it's not listed.
Is there a way to identify file names (incl. path) that are too long?
Remark: I'd like to check the length of each file (incl. Path) in a directory and all its subdirectories.