フルパス名からファイル名を取得          <TOP>


フルパス名からファイル名を抜き出します。

例では、EditBoxにファイルをドラッグ&ドロップしフルパス名を表示させ、「実行」ボタンクリックで抜き出したファイル名のみを表示させています。

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

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

 

 

'================================================================
'= フルパス名からファイル名を取得
'=    (FindFirstFile3.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

Var Shared FullPath As String

'================================================================
'= Chr$(0)を除去
'================================================================
Declare Function TrimNull(StartStr As String) As String
Function TrimNull(StartStr As String) As String
    Var ePos As Integer
   
    ePos = InStr(StartStr, Chr$(0))
   
    If ePos Then
        TrimNull = Left$(StartStr, ePos - 1)
        Exit Function
    End If

    TrimNull = StartStr
End Function

'================================================================
'= フルパス名からファイル名を取り出す
'================================================================
Declare Function GetFileNameFromPath(sFullPath As String) As String
Function GetFileNameFromPath(sFullPath As String) As String
    Var wfd As WIN32_FIND_DATA
    Var hFile As Long
    Var Ret As Long
         
    hFile = Api_FindFirstFile(sFullPath, wfd)
   
    If hFile <> INVALID_HANDLE_VALUE Then
        GetFileNameFromPath = TrimNull(wfd.cFileName)
        Ret = Api_FindClose(hFile)
    End If
End Function

'================================================================
'= シェルドロップされたファイル名を取得
'================================================================
Declare Sub Edit1_DropFiles edecl (ByVal DF As Long)
Sub Edit1_DropFiles(ByVal DF As Long)
    Var CN As Long

    CN = GetDropFileCount(DF)
    FullPath = GetDropFileName(DF, 0)
    Edit1.SetWindowText FullPath
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    FullPath = Edit1.GetWindowText
    Text1.SetWindowText GetFileNameFromPath(FullPath)
End Sub

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