|
发表于 2024-12-12 13:21
来自手机
|
显示全部楼层
本帖最后由 lss001 于 2024-12-13 16:44 编辑
bulesyl 发表于 2024-12-12 09:05
请问关于部分对象模型 是否支持序号索引方式,如何判别?
'fso对象支持索引方式
'需要提供键值key: →fs.Item(key)
'不使用循环建议使用方式3
Sub GetFileName()
Dim fso As Object, fd As Object
Dim fs As Object, fj As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set fd = fso.GetFolder(ThisWorkbook.Path)
Set fs = fd.Files
Dim i&: i = 0 '方式1:Object***
ReDim arr1(1 To fs.Count)
For Each fj In fs
i = i + 1
arr1(i) = fj.Name
Next
Dim fp$, j& '方式2:Item*
ft = Dir(ThisWorkbook.Path & "\*.*")
ReDim arr2(1 To fs.Count - 1)
For j = 1 To fs.Count - 1
If ft <> "" Then
arr2(j) = fs.Item(CStr(ft))
ft = Dir
End If
Next
Dim ws As Object, Ex As Object
Dim arr3$(), ff$ '方式3:Ws/Cmd**
ff = ThisWorkbook.Path
Set ws = CreateObject("WScript.Shell")
Set Ex = ws.Exec("cmd /c dir /a-h /b/s " & _
Chr(34) & ff & Chr(34))
arr3 = Split(Ex.StdOut.ReadAll, vbCrLf)
Set ht = Nothing '释放对象
Set pw = Nothing
Set fso = Nothing
Set fd = Nothing
Set fs = Nothing
End Sub |
|