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 oFiles
		If StrComp(LCase(oFSO.GetExtensionName(oFile)),"dll")=0 Then
			If(oFSO.FileExists("D:\log.txt")) Then
				Set OutputLog = oFSO.OpenTextFile("D:\log.txt",8,False )
				OutputLog.WriteLine oFile.Path
				OutputLog.Close
			Else
				Set OutputLog = oFSO.OpenTextFile("D:\log.txt",2,True)
				OutputLog.WriteLine oFile.Path
				OutputLog.Close
			End If 
		End If 
	Next
	
	Set oSubFolders = oFolder.subfolders
	
	'Recurse the subFolder
	For Each oSubFolder In oSubFolders
		TranverseFile(oSubFolder)
	Next 
	
	Set oFSO = Nothing
	Set oFolder = Nothing 
	Set oSubFolders = Nothing 
	Set oSubFolder = Nothing 
	Set oFiles = Nothing 
	Set oFile = Nothing 
End Function

在这里主要覆盖以下的知识点:

1. 文件的读写

2. 递归遍历文件夹

posted @ 2010-07-10 12:30  GeneJiang  阅读(2663)  评论(0编辑  收藏  举报