フォルダ存在の確認          <TOP>


指定したフォルダが存在するかどうかを確認します。

FindFirstFile ファイルやディレクトリを検索

FindClose ファイル検索ハンドルを解放

 

 

'================================================================
'= フォルダを検索
'=    (FindFirstFile2.bas)
'================================================================
#include "Windows.bi"

#define FILE_ATTRIBUTE_DIRECTORY &H10   'ディレクトリ属性
#define INVALID_HAndLE_VALUE -1         '見つからない場合
#define MAX_PATH 260

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)

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

Var Shared Edit1 As Object
Var Shared Text1 As Object
Var Shared Button1 As Object

Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14
Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 14
Button1.Attach GetDlgItem("Button1") : Button1.SetFontsize 14

'================================================================
'=
'================================================================
Declare Function UnQualifyPath (Folder As String) As String
Function UnQualifyPath(Folder As String) As String

    Folder = Trim$(Folder)                                    '空白削除
   
    If Right$(Folder, 1) = "\" Then
        UnQualifyPath = Left$(Folder, Len(Folder) - 1)        '\削除
    Else
        UnQualifyPath = Folder
    End If
End Function

'================================================================
'=
'================================================================
Declare Function FolderExists (Folder As String) As Integer
Function FolderExists(Folder As String) As Integer
    Var hFile As Long
    Var wfd As WIN32_FIND_DATA
    Var Ret As Long

    '空白・\があれば削除
    Folder = UnQualifyPath(Folder)

    'フォルダ取得
    hFile = Api_FindFirstFile(Folder, wfd)
   
    '有効なディレクトリ属性が設定されるファイルハンドルが戻されたならばフォルダは存在
    FolderExists = (hFile <> INVALID_HAndLE_VALUE) And (wfd.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY)
   
    Ret = Api_FindClose(hFile)
End Function

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var Ret As Integer

    If Len(Edit1.GetWindowText) > 0 Then
        Ret = FolderExists(Edit1.GetWindowText)
        If Ret Then
            Text1.SetWindowText "フォルダは存在します"
        Else
            Text1.SetWindowtext "フォルダは存在しません"
        End If
    End If
End Sub

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