Open文 vs FileSystemObject
| 項目 | Open文 | FileSystemObject |
|---|---|---|
| 参照設定 | 不要(組み込み) | 不要(CreateObjectで可) |
| テキスト読み書き | ◎ 高速・シンプル | ○ オブジェクト経由 |
| ファイル操作(コピー・移動・削除) | △ 別途 FileCopy等を使う | ◎ メソッドで一括 |
| フォルダ操作 | △ MkDir・RmDir のみ | ◎ SubFolders で再帰可能 |
| ファイル属性・プロパティ | △ GetAttr等 | ◎ FileオブジェクトのプロパティでOK |
Open文(テキスト読み書き)
Option Explicit
' ── テキスト書き込み ──
Sub WriteTextFile(ByVal path As String, ByVal content As String)
Dim fn As Integer
fn = FreeFile ' 空きファイル番号を取得
Open path For Output As #fn
Print #fn, content
Close #fn
End Sub
' ── テキスト読み込み(全行)──
Function ReadTextFile(ByVal path As String) As String
Dim fn As Integer
Dim line As String
Dim sb As String
fn = FreeFile
Open path For Input As #fn
Do While Not EOF(fn)
Line Input #fn, line
sb = sb & line & vbCrLf
Loop
Close #fn
ReadTextFile = sb
End Function
FileSystemObject の基本操作
Option Explicit
Sub FsoExample()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim srcPath As String: srcPath = "C:\data\report.xlsx"
Dim destPath As String: destPath = "C:\backup\report.xlsx"
' ── 存在確認 ──
If fso.FileExists(srcPath) Then
' ── コピー ──
fso.CopyFile srcPath, destPath
' ── 移動 ──
' fso.MoveFile srcPath, destPath
' ── 削除 ──
' fso.DeleteFile srcPath
End If
' ── フォルダ作成 ──
If Not fso.FolderExists("C:\backup") Then
fso.CreateFolder "C:\backup"
End If
' ── テキスト書き込み ──
Dim ts As Object
Set ts = fso.CreateTextFile("C:\data\log.txt", True, True) ' True=上書き, True=Unicode
ts.WriteLine "ログ記録: " & Now
ts.Close
Set ts = Nothing
Set fso = Nothing
End Sub
フォルダ再帰処理
Option Explicit
' 指定フォルダ以下の xlsx ファイルをすべて収集
Sub CollectXlsxFiles(ByVal folderPath As String, ByRef results As Collection)
Dim fso As Object
Dim folder As Object
Dim file As Object
Dim sub_ As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
' ── ファイルを処理 ──
For Each file In folder.Files
If LCase(fso.GetExtensionName(file.Path)) = "xlsx" Then
results.Add file.Path
End If
Next file
' ── サブフォルダを再帰処理 ──
For Each sub_ In folder.SubFolders
CollectXlsxFiles sub_.Path, results
Next sub_
Set fso = Nothing
End Sub
' 呼び出し元
Sub Main()
Dim results As New Collection
CollectXlsxFiles "C:\data", results
Dim item As Variant
For Each item In results
Debug.Print item
Next item
End Sub
まとめ
テキストの読み書きだけなら Open 文が簡潔です。ファイル・フォルダの操作全般(コピー・移動・削除・再帰)は FileSystemObject の方が直感的で強力です。
✅ 次の章では…
PART 11 では セル操作の高速化 を解説します。Range vs 配列の一括読み書きとCalculation制御で処理速度を劇的に改善します。