Small Basic >
Curriculum > Online
> Lesson 3.1: File Input and Output
In this lesson, you will learn how to:
Use different properties of the Fileobject.
Use different operations of the Fileobject.
A computer file is a collection of data that your computer stores. In Small Basic, you can work with external files from your program.
By using the File object in Small Basic, you can access information from a file that is stored on your computer. You can also read and write information from and to the file.
The File object includes the following operations and properties:
By using the File object, you can also save and open settings across various sessions of your program.
As you see, you can work with files in many ways by using the File object. Let’s learn about some operations of the File object…
Let’s write a program to gain better understanding of these operations.
FilePath = "C:\temp\TempSubdirectory\my.txt"
TextWindow.WriteLine("Write Content = " +
File.WriteLine(FilePath, 1, "Shakespeare was a great writer."))
TextWindow.WriteLine("Append Content = " +
File.AppendContents(FilePath, "He wrote many plays."))
TextWindow.WriteLine("Read Content = " +
In this example, you specify the path of a file and write a sentence to it by using the
WriteLine operation. Next, you add a sentence to the existing content by using the
AppendContents operation. Finally, you read the entire contents of the file by using the
If you specify a destination that does not exist, the CopyFile operation will try to create it. If a file of the same name already exists, that operation overwrites the existing file. Before you use this operation, verify that a file of
the same name does not already exist in the destination that you specify.
If the CopyFile operation succeeds, “SUCCESS” appears; otherwise, “FAILED” appears.
If the GetFiles operation is successful, it returns the files as an array. Otherwise, “FAILED” appears in the output window.
Let’s write a program to better understand these operations.
sourcefilepath = "C:\temp\TempSubdirectory\my.txt"
directorypath = "C:\temp\"
TextWindow.WriteLine("Copy file Operation:" +
TextWindow.WriteLine("Files in the directory: " +
In this example, you copy the specified source file to the specified destination by using the
CopyFile operation. You also specify the directory path, and you then display the paths of all files in the output window by using the
If the CreateDirectory operation succeeds, “SUCCESS” appears in the output window; otherwise, “FAILED” appears.
If the GetDirectories operation succeeds, a list of directories appears as an array in the output window. Otherwise, “FAILED” appears.
Let’s see how we can apply these operations…
directorypath1 = "C:\temp\Small Basic"
TextWindow.WriteLine("Create Directory: " +
directorypath2 = "C:\temp"
TextWindow.WriteLine("Directories: " +
First, you create a directory by using the CreateDirectory operation.
Next, you get the path of all the directories in the location that you specify by using the
By using the LastError property, you can get details about the most recent file-operation error that occurred in your program. This property is quite useful when an error prevents your program from performing a file operation.
FilePath = "C:\temp\TempSubdirect\my.txt"
TextWindow.WriteLine("Write Line Operation: " +
File.WriteLine(FilePath, 1, "How are you?"))
If File.LastError = "" Then
In this example, you write text to a file at a specific line number that you specify by using the
WriteLine operation of the File object.
Next you get the details of the actual error in the program, if any, by using the
LastError property of the File object.
Congratulations! Now you know how to:
Write a program that performs the following steps:
This solution requires the file to exist with the specified name in the specified location.
To see the answers to these questions, go to the
Answer Key page.
trying to follow slide 5 of the PowerPoint. I'm not sure if this program is meant to be writing files to the temporary folder or whether it