Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
926 views
in Technique[技术] by (71.8m points)

vbscript - 'For Each File In Folder.Files' does not list all files


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

  1. Set HKLMSYSTEMCurrentControlSetControlFileSystemLongPathsEnabled to DWORD 1.

  2. 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>
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...