VBS Example: 如何搜索文件夹中特定类型的文件,并输出到log文件
问题描述:利用VBS遍历某一个文件路径下,所有后缀名为DLL的文件,并将所有搜索得到的文件路径放到一个Log文件中。
VBS代码如下:
Option Explicit
Dim oFSO, oFolder,oSubFolders, oSubFolder, oFiles, oFile
Dim OutputLog,strPathName
strPathName = "D:\"
TranverseFile(strPathName)Function TranverseFile(strPathName)
Set oFSO = CreateObject("scripting.filesystemobject")Set oFolder = oFSO.GetFolder(strPathName)
Set oFiles = oFolder.Files
'Tranverse every file in the specified file path and
'record the file path to the log.txt file.
For Each oFile In oFilesIf StrComp(LCase(oFSO.GetExtensionName(oFile)),"dll")=0 ThenIf(oFSO.FileExists("D:\log.txt")) ThenSet OutputLog = oFSO.OpenTextFile("D:\log.txt",8,False )OutputLog.WriteLine oFile.PathOutputLog.CloseElse
Set OutputLog = oFSO.OpenTextFile("D:\log.txt",2,True)OutputLog.WriteLine oFile.PathOutputLog.CloseEnd IfEnd IfNext
Set oSubFolders = oFolder.subfolders
'Recurse the subFolder
For Each oSubFolder In oSubFoldersTranverseFile(oSubFolder)Next
Set oFSO = NothingSet oFolder = NothingSet oSubFolders = NothingSet oSubFolder = NothingSet oFiles = NothingSet oFile = NothingEnd Function
在这里主要覆盖以下的知识点:
1. 文件的读写
2. 递归遍历文件夹