查找C:下面任意文件夹中的指定文件MyFile.txt
方法一:
Function SearchFolder(objFolder, FileName, Recursive)
Dim oFile, oSubFolder
For each oFile in objFolder.Files
If oFile.Name = FileName Then
'Found the file print the name
Print "File found at - " & oFile.Path
'No more files with same name can exist
Exit For
End if
Next
If Recursive Then
For each oSubFolder in objFolder.SubFolders
'Check in sub folders as well
Call SearchFolder(oSubFolder, FileName, Recursive)
Next
End if
End Function
Set FSO = CreateObject("Scripting.FileSystemObject")
'Get the root folder C:\
Set oRoot = FSO.GetDrive("C:").RootFolder
Call SearchFolder(oRoot, "MyFile.txt", True)
方法二:
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile Where FileName='MyFile' and Extension='txt' and Drive='C:'")
For Each objFile in colFiles
Print objFile.Drive & objFile.Path
Print objFile.FileName & "." & objFile.Extension
Next