Article ID: 186118
Article Last Modified on 7/15/2004
Windows Script Host
Windows NT Option Pack
Microsoft Internet Information Server 3.0
Scripting 3.1 upgrade
Visual Studio 6.0
Visual Basic 6.0
Command1: How to read an existing text file using FileSystemObject
and TextStreamObject.
Command2: How to view file information using FileSystemObject and
the File object.
Command3: How to iterate through folders using FileSystemObject and
the Folder object.
Command4: How to view drive information using FileSystemObject and
the Drive object.
Option Explicit
Private Sub Command1_Click()
'Declare variables.
Dim fso As New FileSystemObject
Dim ts As TextStream
'Open file.
Set ts = fso.OpenTextFile(Environ("windir") & "\system.ini")
'Loop while not at the end of the file.
Do While Not ts.AtEndOfStream
Debug.Print ts.ReadLine
Loop
'Close the file.
ts.Close
End Sub
Private Sub Command2_Click()
Dim fso As New FileSystemObject
Dim f As File
'Get a reference to the File object.
Set f = fso.GetFile(Environ("windir") & "\system.ini")
MsgBox f.Size 'displays size of file
End Sub
Private Sub Command3_Click()
Dim fso As New FileSystemObject
Dim f As Folder, sf As Folder, path As String
'Initialize path.
path = Environ("windir")
'Get a reference to the Folder object.
Set f = fso.GetFolder(path)
'Iterate through subfolders.
For Each sf In f.SubFolders
Debug.Print sf.Name
Next
End Sub
Private Sub Command4_Click()
Dim fso As New FileSystemObject
Dim mydrive As Drive
Dim path As String
'Initialize path.
path = "C:\"
'Get object.
Set mydrive = fso.GetDrive(path)
'Check for success.
MsgBox mydrive.DriveLetter 'displays "C"
End Sub
Keywords: kbhowto kbfileio KB186118