'recursion'에 해당되는 글 2건
- 2008.11.17 vb 서브폴더 구하기
- 2008.10.23 [VB6] 디렉토리 내용 탐색
' ---------------------------------------------
' 서브폴더 구하기(재귀호출)
' 함수사용전에 전역변수로 fso, path가 선언되 있어야 한다.
' strFld를 리턴한다.
' vb Script에서는 배열의 크기가 자동으로 증가되지 않기 때문에
' vbCrLf를 구분자로 사용해서 문자열로 리턴함.
' ---------------------------------------------
function getSubFolder(fso, path, strFld, depth)
Dim fld, sfld, f, fld1, sfld1, files
if not isObject(fso) then
getSubFolder = "fso가 선언되지 않았습니다."
Exit function
end if
set fld = fso.getfolder(path)
set sfld = fld.subfolders
for each f in sfld
set fld1 = fso.getfolder(f.path)
set files = fld1.files
set sfld1 = fld1.subfolders
if Len(strFld) > 0 then strFld = strFld & vbCrLf
strFld
= strFld & depth & "|" & f.name & "|" & f.path
& "|" & files.count & "|" & sfld1.count
if sfld1.count > 0 then Call getSubFolder(fso, f.path, strFld, depth+1)
next
getSubFolder = strFld
end function
Recursively search all files in directory structure
'Get all the matching files in the directory structure.
This tip is useful for VB 3.0, 4.0 and 5.0 - 16 bit and 32 bit
There were some tips published for similar type of function before but
this one does better job, as it does not have any limitation. Moreover,
since this code does not use any API it could be easily ported between
16 - 32 bit applications.
Following procedure DirWalk will let you search the entire directory
structure starting at wherever you specify in the argument.
How to use
============
The procedure should be called as follows
ReDim sArray(0) As String
Call DirWalk("OLE*.DLL", "C:\", sArray)
The procedure would accept the wild card in the first argument which is
search pattern for the file name. The second argument is the location
where to start. Third argument is an array of strings.
The procedure will recursively go to the deepest level in the directory
structure and get all the matching file names with full path in the array
sArray. This array is ReDimed from the function and will have as many
members as matches found.
To use DirWalk you will have to put two extra controls, FileListBox and
DirListBox, on the form. Following procedure is assumed to be on a form
on which there are two controls, FileListBox with name File1 and
DirListBox with name Dir1. Keep them invisible to improve the speed of
search. Putting these additional controls on a form does not cause any
overhead as they are part of basic libray of controls for VB.
Code
====
Sub DirWalk(ByVal sPattern As String, ByVal CurrDir As String, sFound()
As String)
Dim i As Integer
Dim sCurrPath As String
Dim sFile As String
Dim ii As Integer
Dim iFiles As Integer
Dim iLen As Integer
If Right$(CurrDir, 1) <> "\" Then
Dir1.Path = CurrDir & "\"
Else
Dir1.Path = CurrDir
End If
For i = 0 To Dir1.ListCount
If Dir1.List(i) <> "" Then
DoEvents
Call DirWalk(sPattern, Dir1.List(i), sFound)
Else
If Right$(Dir1.Path, 1) = "\" Then
sCurrPath = Left(Dir1.Path, Len(Dir1.Path) - 1)
Else
sCurrPath = Dir1.Path
End If
File1.Path = sCurrPath
File1.Pattern = sPattern
If File1.ListCount > 0 Then 'matching files found in the
directory
For ii = 0 To File1.ListCount - 1
ReDim Preserve sFound(UBound(sFound) + 1)
sFound(UBound(sFound) - 1) = sCurrPath & "\" &
File1.List(ii)
Next ii
End If
iLen = Len(Dir1.Path)
Do While Mid(Dir1.Path, iLen, 1) <> "\"
iLen = iLen - 1
Loop
Dir1.Path = Mid(Dir1.Path, 1, iLen)
End If
Next i
End Sub