Excel/VBAマクロ

Excel VBA フォルダー内ファイル一覧取得

A1セルに記載したフォルダー内の一覧を取得します。エクセルで、フォルダ内にまとめたファイルの目録を作成する際使っています。こちらも重宝します。

Sub フォルダー内ファイル一覧()
Dim f As Object, fld As String
Dim Y As Integer
Y = 0
Y = Y + 1
fld = Cells(Y, 1)    'A1セルに記載したフォルダー内の一覧を取得する
With CreateObject("Scripting.FileSystemObject")
If False = .FolderExists(fld) Then
       MsgBox "フォルダが存在しません"
       End
End If
For Each f In .GetFolder(fld).Files
    Y = Y + 1
    Cells(Y, 1) = f.Name                                          'ファイル名
    If InStr(f.Name, ".") Then
        Cells(Y, 2) = Left(f.Name, InStrRev(f.Name, ".") - 1)         '本体
        Cells(Y, 3) = Mid(f.Name, InStrRev(f.Name, ".") + 1)          '拡張子
    End If
    Cells(Y, 4) = FileLen(fld + "\" + f.Name)                     'ファイルサイズ
    Cells(Y, 5) = Format(FileDateTime(fld + "\" + f.Name), "yyyy-mm-dd hh:mm:ss")
Next f
End With
End Sub
VBAの動作方法がわからない方はこちら参考にしてください
スポンサーリンク
タイトルとURLをコピーしました