Created
March 21, 2012 11:03
-
-
Save wangye/2146209 to your computer and use it in GitHub Desktop.
VBScript Scripting.FileSystemObject enumerate directory folder or file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class FileOperation | |
Private AxFile | |
Private Sub Class_Initialize() | |
Set AxFile = WSH.CreateObject("Scripting.FileSystemObject") | |
End Sub | |
Private Sub Class_Terminate() | |
Set AxFile = Nothing | |
End Sub | |
Private Function GetSubFolders(strFolder) | |
If AxFile.FolderExists(strFolder) Then | |
Dim oFolders | |
Set oFolders = AxFile.GetFolder(strFolder) | |
Set GetSubFolders = oFolders.SubFolders | |
Set oFolders = Nothing | |
Else | |
Set GetSubFolders = Nothing | |
End If | |
End Function | |
Private Function GetSubFiles(strFolder) | |
If AxFile.FolderExists(strFolder) Then | |
Dim oFolders | |
Set oFolders = AxFile.GetFolder(strFolder) | |
Set GetSubFiles = oFolders.Files | |
Set oFolders = Nothing | |
Else | |
Set GetSubFiles = Null | |
End If | |
End Function | |
Public Function EnumFiles(strFolder, fCallBackName, Recursion, Param) | |
EnumFiles = True | |
If Not AxFile.FolderExists(strFolder) Then | |
EnumFiles = False | |
Exit Function | |
End If | |
Dim fCallBack | |
Dim SubFiles, SubFile, SubFolders, SubFolder | |
Set fCallBack = GetRef(fCallBackName) | |
If TypeName(strFolder) = "Folder" Then | |
Set SubFiles = strFolder.Files | |
Else | |
Set SubFiles = GetSubFiles(strFolder) | |
End If | |
For Each SubFile In SubFiles | |
If fCallBack(Me, AxFile, SubFile, Param) Then Exit For | |
Next | |
Set SubFiles = Nothing | |
If Recursion Then | |
Set SubFolders = GetSubFolders(strFolder) | |
For Each SubFolder In SubFolders | |
Call EnumFiles(AxFile.GetAbsolutePathName(SubFolder), _ | |
fCallBackName, Recursion, Param) | |
Next | |
Set SubFolders = Nothing | |
End If | |
Set fCallBack = Nothing | |
End Function | |
Public Function EnumFolders(strFolder, fCallBackName, Recursion, Param) | |
EnumFolders = True | |
If Not AxFile.FolderExists(strFolder) Then | |
EnumFolders = False | |
Exit Function | |
End If | |
Dim fCallBack | |
Dim SubFolders, SubFolder, ChildFolders, ChildFolder | |
Set fCallBack = GetRef(fCallBackName) | |
Set SubFolders = GetSubFolders(strFolder) | |
For Each SubFolder In SubFolders | |
If fCallBack(Me, AxFile, SubFolder, Param) Then Exit For | |
If Recursion Then | |
Set ChildFolders = SubFolder.SubFolders | |
For Each ChildFolder In ChildFolders | |
If fCallBack(Me, AxFile, ChildFolder, Param) Then Exit For | |
Call EnumFolders(AxFile.GetAbsolutePathName(ChildFolder), _ | |
fCallBackName, Recursion, Param) | |
Next | |
Set ChildFolders = Nothing | |
End If | |
Next | |
Set SubFolders = Nothing | |
Set fCallBack = Nothing | |
End Function | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment