ディレクトリ・ファイルを列挙          <TOP>


FindFirstFile 指定したファイル名に一致するファイルやディレクトリを検索
FindNextFile FindFirstFile()関数で検出したファイルの次を検出
FindClose ファイル検索ハンドルをクローズ
GetFileAttributes 指定されたファイルまたはディレクトリの属性を取得

 

'================================================================
'= ディレクトリ・ファイルを列挙
'=    (FindFirstFile5.bas)
'================================================================
#include "Windows.bi"

#define MAX_PATH 260
#define API_FALSE 0
  
#define INVALID_HANDLE_VALUE -1         '見つからない場合
#define ERROR_NO_MORE_FILES 18          'これ以上ファイルは無い
#define vbDirectory 16                  'フォルダ

Type FILETIME
    dwLowDateTime  As Long
    dwHighDateTime As Long
End Type

Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime   As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime  As FILETIME
    nFileSizeHigh    As Long
    nFileSizeLow     As Long
    dwReserved0      As Long
    dwReserved1      As Long
    cFileName        As String * MAX_PATH
    cAlternate       As String * 14
End Type

' 指定したファイル名に一致するファイルやディレクトリを検索
Declare Function Api_FindFirstFile& Lib "Kernel32" Alias "FindFirstFileA" (ByVal lpFileName$, lpFindFileData As WIN32_FIND_DATA)

' FindFirstFile()関数で検出したファイルの次を検出
Declare Function Api_FindNextFile& Lib "Kernel32" Alias "FindNextFileA" (ByVal hFindFile&, lpFindFileData As WIN32_FIND_DATA)

' ファイル検索ハンドルをクローズ
Declare Function Api_FindClose& Lib "Kernel32" Alias "FindClose" (ByVal hFindFile&)

' 指定されたファイルまたはディレクトリの属性を取得
Declare Function Api_GetFileAttributes& Lib "Kernel32" Alias "GetFileAttributesA" (ByVal lpFileName$)

Var Shared Edit1 As Object
Var Shared List1 As Object
Var Shared Button1 As Object

Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14
List1.Attach GetDlgItem("List1") : List1.SetFontSize 14
List1.SetWindowSize 252, 132
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

'================================================================
'=
'================================================================
Declare Function StripNulls(sText As String) As String
Function StripNulls(sText As String) As String
    Var nPos As Long

    StripNulls = sText
    nPos = InStr(sText, Chr$(0))

    If nPos Then StripNulls = Left$(sText, nPos - 1)
    If Len(sText) Then
        If Left$(sText, 1) = Chr$(0) Then
            StripNulls = ""
        End If
    End If
End Function

'================================================================
'=
'================================================================
Declare Function FileExists(sFileName As String) As Integer
Function FileExists(sFileName As String) As Integer
    Var hFile As Long
    Var wfd As WIN32_FIND_DATA
    Var Ret As Long
  
    sFileName = Trim$(sFileName)
    hFile = Api_FindFirstFile(sFileName, wfd)

    If (hFile <> INVALID_HANDLE_VALUE) And (hFile <> ERROR_NO_MORE_FILES) Then
        FileExists = True
    Else If Api_GetFileAttributes(sFileName) <> (-1) Then
        FileExists = True
    End If

    Ret = Api_FindClose(hFile)
End Function

'================================================================
'=
'================================================================
Declare Sub FindFiles(sPath As String)
Sub FindFiles(sPath As String)
    Var wfd As WIN32_FIND_DATA
    Var hFileSearch As Long
    Var sFileName As String
    Var Ret As Long
  
    If FileExists(sPath) Then
        If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"

        hFileSearch = Api_FindFirstFile(sPath & "*.*", wfd)

        If hFileSearch <> INVALID_HANDLE_VALUE Then
            Do
                sFileName = StripNulls(wfd.cFileName)

                If wfd.dwFileAttributes And vbDirectory Then
                    List1.AddString sFileName & " (Dir)"
                Else
                    List1.AddString sFileName
                End If
        
                If Api_FindNextFile(hFileSearch, wfd) = API_FALSE Then
                    Ret = Api_FindClose(hFileSearch)
                    Exit Do
                End If
            Loop
        Else
            List1.AddString "ファイルは見つかりません!"
        End If
    Else
        List1.AddString "パスは無効です!"
    End If
End Sub

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Edit1.SetWindowText "C:\"
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    List1.Resetcontent
    SetMousePointer 2
    FindFiles Edit1.GetWindowText
    SetMousePointer 0
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End