Approach 1: Use NT Object Path-style paths:
See Hans' answer in this QA: Access files with long paths (over 260)
Basically, change your paths to use \?
as a prefix:
Btw, I modified your code as follows:
- Using
Option Explicit
- Removing the
Call
keyword from function invocation statements, note that parentheses must be omitted when not using Call
.
- Using
camelCase
for locals.
Option Explicit ' ALWAYS use Option Explicit!
Dim shortPath: shortPath = "\?C:TEST"
Dim longPath : longPath = "\?C:TESTLONG"
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateFolder shortPath
Dim streamA, streamB
Set streamA = fso.CreateTextFile( shortPath & "" & String(240, "A"), True )
Set streamB = fso.CreateTextFile( shortPath & "" & String(250, "B"), True )
fso.MoveFolder shortPath, longPath
Dim folder
Set folder = fso.GetFolder( longPath )
Dim file
For Each file In folder.Files
Dim msg: msg = "File: " & file.Name & vbCrLf & "Length: " & Len(longPath & "" & File.Name)
Msgbox(msg)
Next
Approach 2:
Set HKLMSYSTEMCurrentControlSetControlFileSystemLongPathsEnabled
to DWORD
1
.
Add or edit the Application Manifest for cscript.exe
and wscript.exe
to add these elements:
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings">
<ws2:longPathAware>true</ws2:longPathAware>
</windowsSettings>
</application>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…