Presentation on theme: "Microsoft® Small Basic"— Presentation transcript:
1Microsoft® Small Basic File Input and OutputEstimated time to complete this lesson: 1 hour
2File Input and Output In this lesson, you will learn how to: Use different properties of the File object.Use different operations of the File object.
3The File object includes the following operations and properties: 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.CreateDirectoryGetDirectoriesWriteLineAppendContentsReadContentsCopyFileGetFilesLastErrorDeleteDirectory
4Operations 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…WriteLineYou can write a line of text at a line number that you specify in a file by using the WriteLine operation.AppendContentsYou can add text that you specify at the end of a file by using the AppendContents operation.ReadContentsYou can read the entire contents of a file by using the ReadContents operation.If the WriteLine or AppendContents operation is successful, “SUCCESS” appears in the output window; otherwise, “FAILED” apears.The WriteLine operation will overwrite the previously written content on the specified line.The ReadContents operation displays the entire contents of the specified file in the output window. This operation is faster when the file is smaller than 1 MB. This operation slows down as the size of the file increases—especially for files that are larger than 10 MB.Code:Writeline:File.WriteLine("C:\Small Basic.txt", 1, "Hello")AppendContents:File.AppendContents("C:\Small Basic.txt","Take Care")ReadContents:File.ReadContents("C:\Small Basic.txt")
5Operations of the File Object Let’s write a program to gain better understanding of these operations.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.outputCode: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))
6Operations of the File Object CopyFileYou can copy the specified file to a destination by using the CopyFile operation.GetFilesYou can get a list of all the files in a directory that you specify by using the GetFiles operation.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.Code:CopyFile:File.CopyFile("C:\Small Basic.txt", "C:\temp")GetFiles:File.GetFiles("C:\Documents and Settings")
7Operations of the File Object Let’s write a program to better understand these operations.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.outputCode: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))
8Operations of the File Object CreateDirectoryBy using this operation, you can create a directory with a name that you specify at a location that you specify.GetDirectoriesBy using this operation, you can get the paths of all the directories in the directory path that you specify.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.Code:CreateDirectory:File.CreateDirectory("C:\File Object")GetDirectories:File.GetDirectories("C:\Documents and Settings")
9Operations of the File Object Let’s see how we can apply these operations…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.outputCode:directorypath1 = "C:\temp\Small Basic"TextWindow.WriteLine("Create Directory: " + File.CreateDirectory(directorypath1))directorypath2 = "C:\temp"TextWindow.WriteLine("Directories: " + File.GetDirectories(directorypath2))
10The 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.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.outputCode:FilePath = "C:\temp\TempSubdirect\my.txt"TextWindow.WriteLine("Write Line Operation: " + File.WriteLine(FilePath, 1, "How are you?"))If File.LastError = "" ThenTextWindow.WriteLine("Operation Successful")ElseTextWindow.WriteLine(File.LastError)EndIf
11Let’s Summarize… Congratulations! Now you know how to: Use different properties of the File object.Use different operations of the File object.
12Show 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.Solution:TextWindow.Write("Enter the name of the new directory: ")DirectoryName = TextWindow.Read()File.CreateDirectory(DirectoryName)filepath = "\\myserver\Share\FileIO.txt"downloadpath = Network.DownloadFile(filepath)If File.CopyFile(downloadpath, DirectoryName) = "SUCCESS" ThenTextWindow.WriteLine("File has been downloaded from the network and copied to: " + DirectoryName) files = File.GetFiles(DirectoryName)TextWindow.WriteLine("This is the content in the file: ")TextWindow.WriteLine(File.ReadContents(files))TextWindow.Write("Enter data to be added in the file:")AppendedData = TextWindow.Read()File.AppendContents(files," " + AppendedData)TextWindow.WriteLine("File content after adding data is as follows: ")EndIf