指定したフォルダの中をサブフォルダも含めて検索します。
下のサンプルをC:¥Windowsで検索すると、上のリストボックス(フォルダ一覧)にWindowsフォルダ以下のフォルダがサブフォルダも含めて出力され、下のリストボックスには上で出力されたフォルダ内の全ファイル(通常ファイルのみ)が出力されます。
Private SubForm_Load()
DimiAs Integer
DimcolFolderAsCollection
DimcolFileAsCollection
SetcolFolder = New Collection
SetcolFile = New Collection
'フォルダの検索(colFolderにはフォルダのパス名が格納されます。)
CallFolderSearch("c:\windows", colFolder)
'ファイルの検索(colFileにはファイルのパス名が格納されます。)
CallFileSearch(colFolder, colFile)
'リストボックスにcolFolderの内容とcolFileの内容を出力します。
LstFolder.Visible =False
LstFile.Visible =False
Fori = 1TocolFolder.Count
LstFolder.AddItem colFolder.Item(i)
Next
Fori = 1TocolFile.Count
LstFile.AddItem colFile.Item(i)
Next
LstFolder.Visible =True
LstFile.Visible =True
SetcolFolder =Nothing
SetcolFile =Nothing
End Sub
SubFolderSearch(strPathAs String, colFolderAsCollection)
Dimstr1As String
Dimstr2As String
IfRight(strPath, 1) <> "\"Then
strPath = strPath & "\"
End If
str1 = Dir(strPath, vbDirectory)
Do Untilstr1 = ""
Ifstr1 = "."Orstr1 = ".."Then
Else
IfGetAttr(strPath & str1)AndvbDirectoryThen
colFolder.Add strPath & str1
CallFolderSearch(strPath & str1, colFolder)
str2 = Dir(strPath, vbDirectory)
Do Untilstr2 = str1
str2 = Dir
Loop
End If
End If
str1 = Dir
Loop
End Sub
SubFileSearch(colFolderAsCollection, colFileAsCollection)
DimiAs Integer
DimstrAs String
DimstrPathAs String
Fori = 1TocolFolder.Count
strPath = colFolder.Item(i)
IfRight(strPath, 1) <> "\"Then
strPath = strPath & "\"
End If
str = Dir(strPath, vbNormal)'通常ファイルを検索
Do Untilstr = ""
colFile.Add strPath & str
str = Dir
Loop
Next
End Sub
DownLoad vbtips068.lzh 3KB (VB6.0)