BackSearchABの1491「何方かお願いします。」を基にし,
ディレクトリを指定し,そこにあるファイルとディレクトリのリストを作成する関数を作成しました。
なお結果はStrListクラス(
http://ab.sinryow.net/strlist.php)に格納されるので,別途こちらをダウンロードしてお使い下さい。
Function
DirList(
szPath As String,
ByRef sl As StrList)(
dwOption As DWord) As Long
szPath どのパスを検索するかを指定します。
sl 結果を格納するStrListクラスを指定します。
dwOption 検索オプションです。
DIRLIST_RECURSIVE ディレクトリを再帰的に検索します(結果はすべて最初に指定したディレクトリを基準にします)。DIRLIST_NO_DIRSと同時には指定できません(DIRLIST_NO_DIRSが優先されます)。
DIRLIST_NO_DIRS 検索結果にディレクトリを含めません。
DIRLIST_NO_FILES 検索結果にファイルを含めません。
ディレクトリ名は,その後ろに「\」をつけた状態で格納されます。
コード:
#include "StrList.sbp"
Const DIRLIST_RECURSIVE =1
Const DIRLIST_NO_DIRS =2
Const DIRLIST_NO_FILES =4
Function DirList(szPath As String, ByRef sl As StrList)(dwOption As DWord) As Long
DirList=_DirList(szPath, "", sl, dwOption)
End Function
Function _DirList(szPath As String, szAddPath As String,
ByRef sl As StrList, dwOption As DWord) As Long
Dim hFind As HANDLE
Dim wfd As WIN32_FIND_DATA
Dim temp As String
' 末尾が「\」で終わっていないパスの場合は付加する
If szPath[Len(szPath)-1]<>92 Then
szPath=szPath+"\"
End If
' 1つ目の要素を得る(同時にハンドル作成)
hFind=FindFirstFile(szPath+"*.*", wfd)
If hFind=INVALID_HANDLE_VALUE Then
_DirList=FALSE
Exit Function
End If
' 次々検索する
Do
If (wfd.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) _
= FILE_ATTRIBUTE_DIRECTORY Then
' ディレクトリが発見された場合
If (dwOption And DIRLIST_NO_DIRS)=0 And _
wfd.cFileName<>"." And wfd.cFileName<>".." Then
temp=MakeStr(wfd.cFileName)+"\"
sl.AddBtm(szAddPath+temp)
If (dwOption And DIRLIST_RECURSIVE)<>0 Then
_DirList(szPath+temp, szAddPath+temp, sl, dwOption)
End If
End If
Else
' ファイルが発見された場合
If (dwOption And DIRLIST_NO_FILES)=0 Then
sl.AddBtm(szAddPath+MakeStr(wfd.cFileName))
End If
End If
Loop While (FindNextFile(hFind, wfd) = TRUE)
' 検索ハンドルを解放する
FindClose(hFind)
_DirList=TRUE
End Function