从递归文件搜索中省略文件夹 [英] Omit Folders From Recursive File Search
本文介绍了从递归文件搜索中省略文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在运行递归文件搜索程序,但我的计算机关闭了.我知道程序在哪个目录停止,有没有办法指定一个递归文件搜索的起始文件夹?例如,假设这是我的结构
I was running a recursive file search procedure, and my computer shut down. I know what directory the procedure stopped at, is there a way I can specify a start folder for a recursive file search? For example, let's say this is my structure
R:\
R:\Test\
R:\Test\Folder1\
R:\Test1\
R:\Test1\Folder1\
R:\Test2\
R:\Test2\Folder2\
如果我希望递归搜索从
if I wanted the recursive search to start at
R:\Test1\Folder1\
流程如何?
Option Compare Database
Sub ScanTablesWriteDataToText()
Dim Fileout As Object
Dim fso As Object
Dim objFSO As Object
Dim accapp As Access.Application
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim colFiles As Collection
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objRegExp As Object
Set objRegExp = CreateObject("VBScript.RegExp")
objRegExp.Pattern = ".jpg"
objRegExp.IgnoreCase = True
Set colFiles = New Collection
RecursiveFileSearch "R:\", objRegExp, colFiles, objFSO
For Each f In colFiles
'do something
Next
Set objFSO = Nothing
Set objRegExp = Nothing
End Sub
Sub RecursiveFileSearch(ByVal targetFolder As String, ByRef objRegExp As Object, _
ByRef matchedFiles As Collection, ByRef objFSO As Object)
Dim objFolder As Object
Dim objFile As Object
Dim objSubFolders As Object
Set objFolder = objFSO.GetFolder(targetFolder)
For Each objFile In objFolder.files
If objRegExp.test(objFile) Then
matchedFiles.Add (objFile)
End If
Next
Set objSubFolders = objFolder.Subfolders
For Each objSubfolder In objSubFolders
RecursiveFileSearch objSubfolder, objRegExp, matchedFiles, objFSO
Next
Set objFolder = Nothing
Set objFile = Nothing
Set objSubFolders = Nothing
End Sub
推荐答案
我会更改您的递归子以包含另外两个参数——一个用于您要查找的文件夹,一个用于指示是否已找到:
I'd change your recursive sub to include two more parameters -- one for the folder you're trying to find, and a boolean to indicate whether or not it's been found:
Sub RecursiveFileSearch(ByVal targetFolder As String, ByRef objRegExp As Object, _
ByRef matchedFiles As Collection, ByRef objFSO As Object, _
ByVal startFolder As String, ByVal found As Boolean)
Dim objFolder As Object
Dim objFile As Object
Dim objSubFolders As Object
Set objFolder = objFSO.GetFolder(targetFolder)
If startFolder = "" Or found Then
For Each objFile In objFolder.files
If objRegExp.test(objFile) Then
matchedFiles.Add (objFile)
End If
Next
End If
Set objSubFolders = objFolder.Subfolders
For Each objSubFolder In objSubFolders
If objSubFolder = startFolder Then
found = True
End If
RecursiveFileSearch objSubFolder, objRegExp, matchedFiles, objFSO, _
startFolder, found
Next
Set objFolder = Nothing
Set objFile = Nothing
Set objSubFolders = Nothing
End Sub
当你调用它时,它会是:
When you call it, it would be:
RecursiveFileSearch "R:\", objRegExp, colFiles, objFSO, "R:\Test1\Folder1\", false
这篇关于从递归文件搜索中省略文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文