FileSystemObject Examples (Visual Basic)
Detailed examples of how to use the file system object.
Note About Guidespot Characters
Due to the text markup language in use on Guidespot, some of the characters namely quotes, double dashes, and apostrophies, are changed to a different character encoding which may be incompatable with your vb compiler. As such, all source code in a form which can be compiled is provided in the link below.
Download Sample Code
Special Thanks to Datafilehost.com for providing a service that allows me to make clean code available for download. You Rock!
CreateTextFile Method
Sub Test1()
Dim str As String
Dim Fs, TFile As Object
str = “Hello World”""
Set Fs = _
CreateObject(“Scripting.FilesystemObject”)
Rem- _
Createtextfile has two parameters – _
1.The path and file name of the _
text file to create _
2.Whether the file should be re-created _
if it already exists (specified by true)
Set TFile = _
Fs.CreateTextFile(“C:\Test.txt”, True)
TFile.WriteLine str
TFile.Close
Set TFile = Nothing
Set Fs = Nothing
End Sub
OpenTextFile Method,Write, and WriteLine Methods
Sub Test2()
Dim str As String
Dim Fs, TFile As Object
Dim Mystring As String
str = “Hello World”
Set Fs = _
CreateObject(“Scripting.FilesystemObject”)
’1 is for reading, 8 is for appending
Set TFile = _
Fs.OpenTextFile(“C:\Test.txt”, 8)
’Writeline puts in a new line
TFile.WriteLine “Hello World”
’Write Not does
TFile.Write “Hello World” & vbNewLine
TFile.Close
Set TFile = Nothing
Set Fs = Nothing
End Sub
ReadAll Method
Sub Test3()
Dim str As String
Dim Fs, TFile As Object
Set Fs = _
CreateObject(“Scripting.FilesystemObject”)
Set TFile = _
Fs.OpenTextFile(“C:\Test.txt”, 1)
str = _
TFile.ReadAll
TFile.Close
Set TFile = Nothing
Set Fs = Nothing
MsgBox str
End Sub
ReadLine Method and AtEndofStream Property
Sub Test4()
Dim str As String
Dim Fs, TFile As Object
Dim Mystring As String
str = “Hello World”""
Set Fs = _
CreateObject(“Scripting.FilesystemObject”)
’1 is for reading, 8 is for appending
Set TFile = _
Fs.OpenTextFile(“C:\Test.txt”, 1)
Do While Not TFile.AtEndOfStream
Mystring = Mystring & vbNewLine _
& TFile.ReadLine
Loop
TFile.Close
Set TFile = Nothing
Set Fs = Nothing
MsgBox Mystring
End Sub
FolderExists and FileExists Methods
Sub Test5()
Dim Fs As Object
Set Fs = _
CreateObject(“Scripting.FilesystemObject”)
MsgBox "File Exists = " & _
Fs.FileExists(“C:\Test.txt”)
MsgBox "Folder Exists = " & _
Fs.FolderExists(“C:\”)
Set Fs = Nothing
End Sub
GetFolder Method and Folder.Path Property
Sub Test7()
Dim Fs, Folder As Object
Set Fs = _
CreateObject(“Scripting.FilesystemObject”)
Set Folder = _
Fs.GetFolder(“C:\”)
MsgBox Folder.Path
Set Folder = Nothing
Set Fs = Nothing
End Sub
GetFile Method
Sub Test8()
Dim Fs, TFile As Object
Set Fs = _
CreateObject(“Scripting.FilesystemObject”)
Set File = Fs.GetFile(“C:\Test.txt”)
MsgBox File.Name
Set File = Nothing
Set Fs = Nothing
End Sub
Guides We Think You'll Like
About The Author
Capital Hill, Denver
I should have taken the blue pill.
Explore
Categories In This Guide
Discussions