Share via


Small Basic Curriculum: Lesson 3.1: File Input and Output


Small Basic
> Curriculum >** **Online > Lesson 3.1: File Input and Output

Estimated time to complete this lesson: 1 hour

File Input and Output

In this lesson, you will learn how to:

Use different properties of the Fileobject.

Use different operations of the Fileobject.

The File Object

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:

CreateDirectory GetDirectories WriteLine
AppendContents ReadContents CopyFile
GetFiles LastError DeleteDirectory

By using the File object, you can also save and open settings across various sessions of your program.

Operations of the File Object

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…

  • WriteLine
    You can write a line of text at a line number that you specify in a file by using the WriteLine operation.

    File.WriteLine("C:\Small Basic.txt", 1, "Hello")

  • AppendContents
    You can add text that you specify at the end of a file by using the AppendContents operation.

    File.AppendContents("C:\Small Basic.txt","Take Care")

  • ReadContents
    You can read the entire contents of a file by using the ReadContentsoperation.

    File.ReadContents("C:\Small Basic.txt")

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 = " + 
 File.ReadContents(FilePath))

 

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 ReadContents operation.

http://msdn.microsoft.com/gg715116.File(en-us,MSDN.10).jpg

  • CopyFile
    You can copy the specified file to a destination by using the CopyFile
    operation.

    File.CopyFile("C:\Small Basic.txt", "C:\temp")

  • GetFiles
    You can get a list of all the files in a directory that you specify by using the GetFiles operation.

    File.GetFiles("C:\Documents and Settings")

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"
destinationfilepath ="C:\temp\TempSubdirectory\Move"
directorypath = "C:\temp\"

TextWindow.WriteLine("Copy file Operation:" + 
 File.CopyFile(sourcefilepath, destinationfilepath))
TextWindow.WriteLine("Files in the directory: " + 
 File.GetFiles(directorypath))

 

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 GetFiles operation.

http://msdn.microsoft.com/gg715116.file_1(en-us,MSDN.10).jpg

  • CreateDirectory
    By using this operation, you can create a directory with a name that you specify at a location that you specify.

    File.CreateDirectory("C:\File Object")

  • GetDirectories
    By using this operation, you can get the paths of all the directories in the directory path that you specify.

    File.GetDirectories("C:\Documents and Settings")

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: " + 
 File.CreateDirectory(directorypath1))
directorypath2 = "C:\temp"
TextWindow.WriteLine("Directories: " + 
 File.GetDirectories(directorypath2))

 

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 GetDirectories operation.

http://msdn.microsoft.com/gg715116.file_2(en-us,MSDN.10).jpg

The LastError Property

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
  TextWindow.WriteLine("Operation Successful")
Else
  TextWindow.WriteLine(File.LastError)
EndIf

 

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.

http://msdn.microsoft.com/gg715116.file_3(en-us,MSDN.10).jpg

Let’s Summarize…

Congratulations! Now you know how to:

  • Use different properties of the File object.
  • Use different operations of the File object.

Show What You Know

Write a program that performs the following steps:

  • Requests a suitable name for a directory from the user, and creates a directory of that name.
  • Downloads a file from the network, and copies it to the new directory.
  • Displays the contents of the downloaded file in the text window.
  • Accepts additional content from the user, and adds it to the file.
  • Displays the final content from the file in the text window.

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.

Next Lesson

PowerPoint Downloads