EziData Solutions

Web, NET and SQL Server

Working with Text Files Tutorials

Reading and writing from text files may seem antiquated to the modern developer, after all didn’t we invent database management systems to avoid using flat files in the first place? There are however many times when it is useful and even absolutely necessary to read and write to text files especially when transferring data between legacy systems or proprietary systems that don’t offer direct access to their underlying databases.

Thankfully the FileSystemObject contains a number of excellent methods for opending, reading and writing to text files.

Writing to Text Files

You will find quite a number of examples on the web where the FileSystemObject is not used to write to a text file, which of course works, but to be thorough in our discussion about the FileSystemObject class we’ll look at the slightly more complex using a TextStream.

When you were in the Object Browser you may have noticed that the both the CreateTextFile and OpenTextFile functions of the FileSystemObject class returned a TextStream. We can use this TextStream to add additional lines of data to a new or existing text file.

Creating and Writing to a Text File

'create a new text file and write a line of text
Dim strFileName As String
strFileName = "C:\Temp\MyWorld.txt"

'declare the variables for use with the Scripting library
Dim fso As New Scripting.FileSystemObject
Dim fsoStream As Scripting.TextStream

'create a text file - overwriting any previous file
Set fsoStream = fso.CreateTextFile(strFileName, True)

'write a line to the file
With fsoStream

.WriteLine "Hello World!"

End With

When you were in the Object Browser you may have noticed that the both the CreateTextFile and OpenTextFile functions of the FileSystemObject class returned a TextStream. We can use this TextStream to add additional lines of data to a new or existing text file.

Opening and Writing to a Text File

'open an existing text file and write a line of text
Dim strFileName As String
strFileName = "C:\Temp\MyWorld.txt"

'declare the variables for use with the Scripting library
Dim fso As New Scripting.FileSystemObject
Dim fsoStream As Scripting.TextStream

'create a text file - overwriting any previous file
Set fsoStream = fso.OpenTextFile(strFileName, ForWriting, True)

'write a line to the file
With fsoStream

.WriteLine "Hello World!"

End With

'close the text file
fsoStream.Close

If you run the example above you will notice that no matter how many times you run it, you only end up with one Hello World! in the text file. This is because the ForWriting option, despite the sensible sounding name actually clears the test file when you open it. Not a particularly useful function if you were hoping to keep your previous information. In this case you will need to use the ForAppending option.

Posted: Oct 28 2006, 01:06 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: VBA | Access | VBA

Getting Information about your Files

Having come this far, with a complete list of Drives and Folders, the next logical step is to see if we can find out anything useful about what Files our Folders contain. Thankfully once again the FileSystemObject contains a very useful method called Files that returns a collection of files within the current folder.

Building on the example we used when dealing with Folders using the FileSystemObject, we can now gather a raft of information about the files located in a particular folder using the Files collection of the FileSystemObject.

'declare the starting or root folder – you could get this from the Drives
Dim strRoot As String
strRoot = "C:\"

'declare the variables for use with the Scripting library
Dim fso As New Scripting.FileSystemObject
Dim parent As Scripting.Folder
Dim children As Scripting.Folders
Dim child As Scripting.Folder
Dim fl As File

'get the root folder from fso
Set parent = fso.GetFolder(strRoot)

'get the subfolders contained within the root folder
Set children = parent.SubFolders

'iterate through the subfolder under the root and display some data
For Each child In children

'iterate through the files
For Each fl In child.Files
Debug.Print fl.Name & " " & fl.Type
Next

Next

Of course, sometimes you already know the location of the file you want information about, so the FileSystemObject includes another function called GetFile that takes as its parameter the full path and filename of the file.

'declare the starting or root folder – you could get this from the Drives
Dim strFilePath As String strFilePath = "C:\Temp\MyWorld.txt"

'declare the variables for use with the Scripting library
Dim fso As New Scripting.FileSystemObject
Dim fl As File

'get the file from fso
Set fl = fso.GetFile(strFilePath)

'display some information about the file
Debug.Print fl.Name & " " & fl.Type & " " & fl.Size

'TODO: add some code to move, copy or delete the file
'you can even open it as a TextStream to read the contents

Posted: Oct 21 2006, 00:29 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Access | VBA

Getting Information about your Folders

Just knowing the drive letters on your computer isn’t very exciting, but using this information to display a list of folders starts getting a little more useful. The GetFolder method of the FileSystemObject returns a Folder object, which contains a whole lot of useful information about the folder, including its name, size and a collection of subfolders.

You may on occasion need to display or work with a list of sub-folders located within a parent folder. Thankfully, as with finding out what Drives your computer has access to, working with fodlers is easy using the Folders property of the FileSystemObject.

'declare the starting or root folder – you could get this from the Drives
Dim strRoot As String
strRoot = "C:\"

'declare the variables for use with the Scripting library
Dim fso As New Scripting.FileSystemObject
Dim parent As Scripting.Folder
Dim children As Scripting.Folders
Dim child As Scripting.Folder

'get the root folder from fso
Set parent = fso.GetFolder(strRoot)

'get the subfolders contained within the root folder
Set children = parent.SubFolders

'iterate through the subfolder under the root and display some data
For Each child In children
Debug.Print child.name
Next

Posted: Oct 14 2006, 00:27 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Access | VBA

Getting Information about your Drives

As you have no doubt discovered as you carefully analysed the Scripting library in the Object Browser, there are far more items listed than just the TextStream class and the OpenTextFile method. Using the FileSystemObject you can retrieve a wealth of information about the current computer, including information about available drives, folders and files.

You may on occasion need to display a list of drives the current computer user has access to, so that you can store information about the location of various files used within your Access database. This is very easy using the Drives property of the FileSystemObject.

'declare the variables for use with the Scripting library

Dim fso As New Scripting.FileSystemObject
Dim drvs As Scripting.Drives
Dim drv As Scripting.Drive

'get the drives from fso
Set drvs = fso.Drives

'iterate through the drives and display some data
For Each drv In drvs

Debug.Print drv.DriveLetter & " " & drv.ShareName

Next

You will notice that there is a myriad of properties that you can discover about each Drive, including AvailableSpace and FileSystem, such as FAT and NTSC. Try a few out in the Debug line for the example above and then impress you friends!

Posted: Oct 07 2006, 10:25 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Access | VBA