wrote :: 2005.01.18
| プロパティ | |
|---|---|
| AtEndOfLine | ファイルポインタが終端かどうかを返します |
| AtEndOfStream | ファイルポインタが終端かどうかを返します |
| Column | ファイルポインタの文字位置を返します |
| Line | ファイルポインタの行位置を返します |
| メソッド | |
| Close | テキストファイルを閉じます |
| Read | 指定した文字数だけ読み込みます |
| ReadAll | すべての文字を読み込みます |
| ReadLine | 1行分の文字を読み込みます |
| Skip | 指定した文字数だけスキップします |
| SkipLine | 1行分スキップします |
| Write | 指定した文字を書き込みます |
| WriteBlankLine | 改行を書き込みます |
| WriteLine | 1行分の文字を書き込みます |
Sub test72()
Dim FSO, buf As String
Set FSO = CreateObject("Scripting.FileSystemObject")
''C:\Work\Sample.txtの最後まで読み込んだかどうか表示します
With FSO.GetFile("C:\Work\Sample.txt").OpenAsTextStream
buf = .ReadLine
If .AtEndOfLine Then
MsgBox "最後まで読み込みました"
Else
MsgBox "最後まで読み込んでいません"
End If
.Close
End With
Set FSO = Nothing
End Sub
Sub test73()
Dim FSO, buf As String
Set FSO = CreateObject("Scripting.FileSystemObject")
''C:\Work\Sample.txtの最後まで読み込んだかどうか表示します
With FSO.GetFile("C:\Work\Sample.txt").OpenAsTextStream
buf = .ReadLine
If .AtEndOfStream Then
MsgBox "最後まで読み込みました"
Else
MsgBox "最後まで読み込んでいません"
End If
.Close
End With
Set FSO = Nothing
End Sub
Sub test74()
Dim FSO, buf As String
Set FSO = CreateObject("Scripting.FileSystemObject")
''次の読み込み位置を表示します
With FSO.GetFile("C:\Work\Sample.txt").OpenAsTextStream
buf = .Read(3)
MsgBox "次は、" & .Column & "文字目から読み込みます"
.Close
End With
Set FSO = Nothing
End Sub
Sub test75()
Dim FSO, buf As String
Set FSO = CreateObject("Scripting.FileSystemObject")
''次の読み込み位置を表示します
With FSO.GetFile("C:\Work\Sample.txt").OpenAsTextStream
buf = .ReadLine
MsgBox "次は、" & .Line & "行目から読み込みます"
.Close
End With
Set FSO = Nothing
End Sub
Sub test76()
Dim FSO, buf As String
Set FSO = CreateObject("Scripting.FileSystemObject")
With FSO.GetFile("C:\Work\Sample.txt").OpenAsTextStream
buf = .ReadLine
MsgBox "次は、" & .Line & "行目から読み込みます"
''ファイルを閉じます
.Close
End With
Set FSO = Nothing
End Sub
Sub test77()
Dim FSO, buf As String
Set FSO = CreateObject("Scripting.FileSystemObject")
''C:\Work\Sample.txtの先頭から2文字を読み込んで表示します
With FSO.GetFile("C:\Work\Sample.txt").OpenAsTextStream
buf = .Read(2)
MsgBox buf
.Close
End With
Set FSO = Nothing
End Sub
Sub test78()
Dim FSO, buf As String
Set FSO = CreateObject("Scripting.FileSystemObject")
''C:\Work\Sample.txtの全ての文字を読み込んで表示します
With FSO.GetFile("C:\Work\Sample.txt").OpenAsTextStream
buf = .ReadAll
MsgBox buf
.Close
End With
Set FSO = Nothing
End Sub
Sub test79()
Dim FSO, buf As String
Set FSO = CreateObject("Scripting.FileSystemObject")
''C:\Work\Sample.txtの先頭から1行読み込んで表示します
With FSO.GetFile("C:\Work\Sample.txt").OpenAsTextStream
buf = .ReadLine
.Close
End With
MsgBox buf
Set FSO = Nothing
End Sub
Sub test80()
Dim FSO, buf As String
Set FSO = CreateObject("Scripting.FileSystemObject")
''先頭から2文字と、6文字目から4文字を読み込んで表示します
With FSO.GetFile("C:\Work\Sample.txt").OpenAsTextStream
buf = .Read(2)
.Skip 3
buf = buf & .Read(4)
.Close
End With
MsgBox buf
Set FSO = Nothing
End Sub
Sub test81()
Dim FSO, buf As String
Set FSO = CreateObject("Scripting.FileSystemObject")
''1行目と3行目を読み込んで表示します
With FSO.GetFile("C:\Work\Sample.txt").OpenAsTextStream
buf = .ReadLine & vbCrLf
.SkipLine
buf = buf & .ReadLine
.Close
End With
MsgBox buf
Set FSO = Nothing
End Sub
Sub test82()
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
''「田中」と「亨」を続けて書き込みます
With FSO.GetFile("C:\Work\Sample.txt").OpenAsTextStream(8)
.Write "田中"
.Write "亨"
.Close
End With
Set FSO = Nothing
End Sub
Sub test83()
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
''「田中」と「亨」の間に2行の改行を書き込みます
With FSO.GetFile("C:\Work\Sample.txt").OpenAsTextStream(8)
.Write "田中"
.WriteBlankLines 3
.Write "亨"
.Close
End With
Set FSO = Nothing
End Sub
Sub test84()
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
''「田中」と「亨」を1行ごとに書き込みます
With FSO.GetFile("C:\Work\Sample.txt").OpenAsTextStream(8)
.WriteLine "田中"
.WriteLine "亨"
.Close
End With
Set FSO = Nothing
End Sub