19.04.2017 Views

Learn to Program with Small Basic

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

To keep the code simple, we didn’t add error-checking code. We also<br />

assumed that the input file was properly formatted: each line contained a<br />

user’s first and last names separated by a single space. Table 19-2 shows an<br />

example input file for this program and the output file.<br />

Table 19-2: Creating Login Names<br />

User’s name<br />

Tina Fey<br />

Jimmy Fallon<br />

David Letterman<br />

Jay Leno<br />

Amy Poehler<br />

Login name<br />

tfey<br />

jfallo<br />

dlette<br />

jleno<br />

apoehl<br />

TRY IT OUT 19-2<br />

Write a program that reads an input file and counts the number of lines, characters,<br />

and spaces it contains.<br />

File Management<br />

In addition <strong>to</strong> the methods that let you perform file I/O, the File object<br />

also provides a couple of methods related <strong>to</strong> file and direc<strong>to</strong>ry management.<br />

Using these methods, you can copy and delete files, create and delete<br />

direc<strong>to</strong>ries, and list files and direc<strong>to</strong>ries from your program.<br />

Copying and Deleting Files<br />

You can use the CopyFile() method <strong>to</strong> create a copy of an existing file. This<br />

method takes the pathnames of the source file and the destination file as<br />

arguments. The source file isn’t affected by this operation. If the operation<br />

is successful, the method returns "SUCCESS". Otherwise, it returns "FAILED".<br />

If the destination path points <strong>to</strong> a location that doesn’t exist, the method<br />

attempts <strong>to</strong> create it au<strong>to</strong>matically. For example, look at the following code:<br />

srcPath = "C:\Temp\Test1.txt"<br />

' Path of the source file<br />

dstPath = "C:\Temp\Temp1\Temp2\Test1.txt" ' Path of the destination file<br />

File.CopyFile(srcPath, dstPath)<br />

If the subfolders Temp, Temp1, and Temp2 don’t exist, CopyFile() attempts<br />

<strong>to</strong> create all the direc<strong>to</strong>ries in the destination path, beginning <strong>with</strong> the<br />

root. When you run this code, you’ll have two copies of the Test1.txt file: the<br />

original source file and the duplicate file under C:\Temp\Temp1\Temp2.<br />

298 Chapter 19

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!