VBA ファイルオブジェクト 小ネタ

ファイル存在確認(FileExists)

' === ▼ 1. ファイル存在確認(FileExists) ===
Public Sub Tip_FileExists()
    Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim path As String: path = "C:\MyTest\sample.xlsx"

    If fso.FileExists(path) Then
        Debug.Print "ファイルあり"
    Else
        Debug.Print "ファイルなし"
    End If
End Sub

フォルダ存在確認(FolderExists)

' === ▼ 2. フォルダ存在確認(FolderExists) ===
Public Sub Tip_FolderExists()
    Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim path As String: path = "C:\MyTest"

    If fso.FolderExists(path) Then
        Debug.Print "フォルダあり"
    Else
        Debug.Print "フォルダなし"
    End If
End Sub

ファイルサイズ取得

' === ▼ 3. ファイルサイズ取得 ===
Public Sub Tip_FileSize()
    Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim path As String: path = "C:\MyTest\sample.xlsx"
    Dim fileObj As Object

    If fso.FileExists(path) Then
        Set fileObj = fso.GetFile(path)
        Debug.Print "ファイルサイズ: " & fileObj.Size & " byte"
    End If
End Sub

作成・更新・アクセス日時取得

' === ▼ 4. 作成・更新・アクセス日時取得 ===
Public Sub Tip_FileDates()
    Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim filePath As String: filePath = "C:\MyTest\sample.xlsx"
    Dim fileObj As Object

    If fso.FileExists(filePath) Then
        Set fileObj = fso.GetFile(filePath)
        Debug.Print "作成日: " & fileObj.DateCreated
        Debug.Print "更新日: " & fileObj.DateLastModified
        Debug.Print "アクセス日: " & fileObj.DateLastAccessed
    End If
End Sub

ファイルのコピーと削除

' === ▼ 5. ファイルのコピーと削除 ===
Public Sub Tip_CopyDeleteFile()
    Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim src As String: src = "C:\MyTest\sample.xlsx"
    Dim dest As String: dest = "C:\MyTest\sample_copy.xlsx"

    If fso.FileExists(src) Then
        fso.CopyFile src, dest, True
        Debug.Print "コピー完了"

        ' 削除(必要に応じて)
'         fso.DeleteFile dest
'         Debug.Print "削除完了"
    End If
End Sub

空フォルダチェック

' === ▼ 6. 空フォルダチェック ===
Public Sub Tip_CheckIfFolderIsEmpty()
    Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim path As String: path = "C:\MyTest"
    Dim folder As Object

    If fso.FolderExists(path) Then
        Set folder = fso.GetFolder(path)
        If folder.Files.Count = 0 And folder.SubFolders.Count = 0 Then
            Debug.Print "空のフォルダです"
        Else
            Debug.Print "中身あり"
        End If
    End If
End Sub

拡張子やファイル名・パス分解

' === ▼ 7. 拡張子やファイル名・パス分解 ===
Public Sub Tip_ParseFilePath()
    Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim path As String: path = "C:\MyTest\sample.xlsx"

    Debug.Print "拡張子: " & fso.GetExtensionName(path)
    Debug.Print "ファイル名のみ: " & fso.GetFileName(path)
    Debug.Print "フォルダ部分: " & fso.GetParentFolderName(path)
End Sub

拡張子付きファイル一覧をループで取得

' === ▼ 8. 拡張子付きファイル一覧をループで取得 ===
Public Sub Tip_FSO_FilteredList()
    Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim folder As Object, f As Object
    Set folder = fso.GetFolder("C:\MyTest")

    Debug.Print "★ .csvファイル一覧"
    For Each f In folder.Files
        If LCase(fso.GetExtensionName(f.Name)) = "csv" Then
            Debug.Print f.Name
        End If
    Next
End Sub

フォルダがなければ作成

' === ▼ 9. フォルダがなければ作成 ===
Public Sub Tip_FSO_ConditionalMkDir()
    Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim path As String: path = "C:\MyTest\TempFolder"

    If Not fso.FolderExists(path) Then
        fso.CreateFolder path
        Debug.Print "フォルダ作成"
    Else
        Debug.Print "既に存在"
    End If
End Sub

隠しファイル・隠しフォルダ属性を判定

' === ▼ 10. 隠しファイル・隠しフォルダ属性を判定 ===
Public Sub Tip_FSO_HiddenCheck()
    Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim path As String: path = "C:\MyTest\HiddenSample"
    Dim folder As Object

    If fso.FolderExists(path) Then
        Set folder = fso.GetFolder(path)
        If folder.Attributes And 2 Then
            Debug.Print "隠しフォルダです"
        Else
            Debug.Print "通常フォルダです"
        End If
    End If
End Sub

コメント