指定したディレクトリにあるファイル数の取得方法
Posted: 2006年4月06日(木) 01:56
リストビューにファイル一覧を表示するファイラーを作っているのですが、
指定したディレクトリにある、ファイル数を取得するにはどうすればいいのでしょうか。
指定したディレクトリにある、ファイル数を取得するにはどうすればいいのでしょうか。
コード: 全て選択
'前の文字へのポインタを返却(マルチバイト対応)
Declare Function CharPrev Lib "user32" Alias "CharPrevA" (lpszStart As *Byte,lpszCurrent As *Byte) As *Byte
'文字列が本当に \ で終わっているか判定する(マルチバイト対応)
Function EndYen(CheckStr As String) As Long
Dim Ptr As *Byte,Ptr2 As *Byte
Ptr=StrPtr(CheckStr):Ptr2=Ptr+Len(CheckStr)
EndYen=-(GetByte(Ptr2-1)=&H5C And Ptr2-CharPrev(Ptr,Ptr2)=1)
End Function
'ディレクトリ内のファイル数取得
Function GetFileCount(strDirectory As String) As Long
Dim hFind As HANDLE
Dim udtFindData As WIN32_FIND_DATA
Dim iCount As Long
Dim lpMsgBuf As BytePtr
With udtFindData
iCount=0
'ワイルドカード文字列生成
If EndYen(strDirectory) Then
'\ で終わっているとき
strDirectory=strDirectory & "*"
Else
'\ で終わっていないとき
strDirectory=strDirectory & "\*"
End If
hFind=FindFirstFile(strDirectory,udtFindData)
If hFind=INVALID_HANDLE_VALUE Then
'Error
GetFileCount=-1
Else
Do
'自分自身 & 親ディレクトリは除外してカウント
If lstrcmp(.cFileName,".")<>0 And lstrcmp(.cFileName,"..")<>0 Then iCount++
Loop While FindNextFile(hFind,udtFindData)
FindClose(hFind)
GetFileCount=iCount
End If
End With
End Function