Text Files

Text files are also sometimes known as sequential files


Accessing Files

There are three ways you can access file:
Sequential - This allows reading and writing of individual characters or entire lines. You can start at the beginning and read forwards until you reach the end.
Random - This allows you to read and write record that all have the same length.
Binary - This allows you to read and write to specific byte positions within a file.


Private Function FileNameOnly(sFullPath As String) As String 
Dim I As Integer
Dim length As Integer
Dim temp As String
Length = Len(sFullPath)
temp = ""

For I = length to 1 Step -1
   If Mid(sFullPath,I,1) = Application.PathSeparator Then
      FileNameOnly = temp
      Exit Function
   End If
   temp = Mid(sFullPath,I,1) & temp
Next i
FileNameOnly = sFullPath
End Function

Title

The Dir function attempts to match its input argument against existing files.
This function can be used with valid wild cards under Windows for matches such as "*.xls".
If it finds a match, it returns the first match found and can be called again without an input argument to get subsequent matches.


Does a file exist

If Dir("C:\Temp.xls") <> "" Then 
'then the file exists
End If

Dim sFolderPath As String 
If Dir(sFolderPath, vbDirectory) <> "" Then 'folder path exists

Else
'folder path does not exist
End If

Text Streams

There are three ways to access a file
Sequential
Random
Binary


Sequential Access

This methods starts reading from the start and reads a line at a time
The Open statement is used to open your file.


Additional

You can specify the exact position in a file using the Seek() function.


Reading Files


INPUT - Function - Reads a number of characters from a file.
INPUT - Statement - Reads a line as a series of variables with each variable seperated by a comma.
LINE INPUT - Reads a line as a series of variables with each variable seperated by a carriage return and line feed.
GET - Read data from a text file into a record.


The most way to access a file is to assign it a unique file number.


You can read from a text file direct into an array

Function ExtractPath(sFullPath As String) As String 
SpecLen = Len(Spec)
For I = SpecLen To 1 Step -1
   If Mid(sFullPath,I,1) = "\" Then
      ExtractPath = Left(sFullPath,I - 1)
      Exit Function
   End If
Next i
ExtractPath = ""
End Function

Alternatively you can use the InStrRev function.

Function ExtractPath(sFullPath As String) As String 
   ExtractPath = Left(sFullPath, InstrRev(sFullPath,"\") )
End Function

Writing Files


WRITE - Writes a series of values, with each value seperated by a comma and enclosed in speech marks.
PRINT - Writes a series of values with each value seperated by a tab character.
PUT - Writes data from a record into a text file.


Three types of text file are supported
CSV - comma seperated value - columns of data are seperated by a comma and each row is seperated by a carriage return.
PRN - columns of data are aligned by character position and each row of data ends in a carriage return.
TXT - tab delimited - columns of data are seperated by tabs and each row is separated by a carriage return.


Writing data to a textfile

Public Sub WritingData 
Dim iFileNumber As Integer
Dim sTheText As String
Dim sFileName As String

   iFileNumber = FreeFile
   sTheText = "The text that I want to add to the file"

   sFileName = "C:\Temp\Text.txt"
   Open sFileName For Output As #iFileNumber
   Print #iFileNumber, sTheText
   Close #iFileNumber
End Sub

Copying Files


Deleting Files




© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext