1.利用WMI:
Set oFSO = CreateObject("SCripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile("a.txt",2,True)
For Each x In GetObject("winmgmts:").execquery("select * from CIM_DATAFILE where extension = 'txt' and drive = 'c:'")
oFile.WriteLine x.name
Next
oFile.Close
Set oFile = Nothing
Set oFSO = Nothing
2,传统的Scripting.FileSystemObject对象:
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("c:\")
Set oFile = oFSO.OpenTextFile("a.txt",2,True)
GetFileName oFolder
oFile.Close
Set oFile = Nothing
Set oFSO = Nothing
Sub GetFileName(oFolder)
For Each tmpFile In oFolder.Files
If LCase(Right(tmpFile.Name,4)) = ".txt" Then
oFile.WriteLine tmpFile.Path
End If
Next
For Each tmpFolder In oFolder.SubFolders
GetFileName(tmpFolder)
Next
End Sub
其他一些组件也能完成类似的功能(Shell.Application等等),不过个人认为这上面两种方法无疑简单许多.