'For Each File In Folder.Files' 没有列出所有文件
'For Each File In Folder.Files' does not list all files
For Each File In Folder.Files
只要文件名(包括路径)的长度在 Windows 的限制内(259 个字符)就可以正常工作。但如果文件名(包括路径)较长,则不起作用。
这是一个例子:
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
通过将文件夹“C:\TEST”重命名为“C:\TESTLONG”,文件名“AAA...”(包括路径)的长度为 252 (12+240) 个字符,这仍然少于 260 个字符。因此列出了这个文件。
但是文件名“BBB...”(包括路径)的长度现在是262(12+250)个字符,超过了259个字符,也就是说没有列出来。
有没有办法识别太长的文件名(包括路径)?
备注:我想查看目录及其所有子目录中每个文件(包括路径)的长度。
方法 1:使用 NT 对象路径样式路径:
请参阅 Hans 在此 QA 中的回答:
基本上,更改您的路径以使用 \?\
作为前缀:
顺便说一句,我修改了你的代码如下:
- 使用
Option Explicit
- 去掉函数调用语句中的
Call
关键字,注意不使用Call
. 时必须省略括号
- 本地人使用
camelCase
。
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
方法二:
将HKLM\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled
设置为DWORD
1
。
添加或编辑 cscript.exe
和 wscript.exe
的应用程序清单以添加这些元素:
<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>
For Each File In Folder.Files
只要文件名(包括路径)的长度在 Windows 的限制内(259 个字符)就可以正常工作。但如果文件名(包括路径)较长,则不起作用。
这是一个例子:
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
通过将文件夹“C:\TEST”重命名为“C:\TESTLONG”,文件名“AAA...”(包括路径)的长度为 252 (12+240) 个字符,这仍然少于 260 个字符。因此列出了这个文件。
但是文件名“BBB...”(包括路径)的长度现在是262(12+250)个字符,超过了259个字符,也就是说没有列出来。
有没有办法识别太长的文件名(包括路径)?
备注:我想查看目录及其所有子目录中每个文件(包括路径)的长度。
方法 1:使用 NT 对象路径样式路径:
请参阅 Hans 在此 QA 中的回答:
基本上,更改您的路径以使用 \?\
作为前缀:
顺便说一句,我修改了你的代码如下:
- 使用
Option Explicit
- 去掉函数调用语句中的
Call
关键字,注意不使用Call
. 时必须省略括号
- 本地人使用
camelCase
。
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
方法二:
将
HKLM\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled
设置为DWORD
1
。添加或编辑
cscript.exe
和wscript.exe
的应用程序清单以添加这些元素:<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>