Processing File Contents

[ LiB ]

Processing File Contents

It's not much harder to read and process a text file using JScript than it is to create and write to it. The first thing that you have to do is use the FileSystemObject object's FileExists() method to perform a check to determine whether or not the file you want to read exists. If it does exist, your script can open and begin to read it. The next thing that your script will need to do is to make sure that the file contains some data to process. You can do this using the TextStream object's AtEndOfStream property. In addition, the AtEndOfStream property will need to be checked just before each additional read operation to make sure that the end of the file has not been reached.

The following example demonstrates how to use JScript to read and process the output file created by the previous script:

 //************************************************************ ************* //Script Name: Script 6.6.js //Author: Jerry Ford //Created: 09/14/03 //Description: This JScript demonstrates how to read and //process data stored in text files. //************************************************************ ************* //Initialization Section //Instantiate the FileSystemObject var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); //Define variable that will be use to set up an object //reference to the input file var inputLine //Main Processing Section if (fsoObject.FileExists("c:\MyDocs\Sample.txt")) {    var inputFile = fsoObject.OpenTextFile("c:\MyDocs\ Sample.txt", 1, "True");   ProcessTheFile() } CloseTheFile() //Terminate the script's execution WScript.Quit(); //Procedure Section //Verify that the input file exists and process it function ProcessTheFile() {   while (!inputFile.AtEndOfStream) {     inputLine = inputFile.ReadLine();     WScript.Echo(inputLine);   } } function CloseTheFile() {   inputFile.Close(); } 

This script begins by instantiating the FileSystemObject . Next it defines a variable that will be used later to set up an object reference to the c:\MyDocs\Sample.txt file. The script then checks to see if the Sample.txt file exists. If it does exist, the file object reference is established and the ProcessTheFile() function is called. This function uses a while loop to read data a line at a time from the input file. It iterates until the end of file is reached. Once the entire file has been read, the CloseTheFile() function executes, and the script terminates is own execution.

Figure 6.11 shows the output that is displayed when this script is run from the Windows command prompt.

Figure 6.11. Creating a JScript that reads and displays the contents of an entire file


Skipping Lines when Processing Files

In the preceding example, the script read a report stored in an input file from top to bottom, including the report's header section. You can set up your script so that it skips portions of input files when processing them. For example, in the preceding example, you might want to modify the script to skip the first five lines, which consists of a header and blanks lines.

The FileSystemObject provides two methods for skipping portions of input files:

  • Skip() . Skips the processing of a specified number of characters

  • SkipLine() .Skips the processing of a single line

Using the Skip() method, you can skip a given number of characters when processing data stored in formatted reports that have known column positions . Typically, you'd use this method in conjunction with the FileSystemObject object's Read() methods, which I'll go over in just a bit. The following JScript statement demonstrates how to use this method to skip the processing of the next 10 characters in an input file with an object reference of inputFile :


Unlike the Skip() method, the SkipLine() method does not accept an argument that enables you to specify the number of lines to be skipped . Instead, you can use it only to skip one line at a time, as demonstrated here:


However, you can always wrap this statement inside a loop to skip multiple lines. For example, the following JScript statements set up a loop that will skip the next 10 lines in the input file:

 for (i=0; i<10; i++) {   inputFile.skipLine() } 

Processing Formatted Data

For files that contain known starting and ending column positions, you may want to read the data stored in the input file using the FileSystemObject object's Read() method. For example, suppose that you had an input file called employee.txt that contained the following data:

  -  Company Employee File  -  Last Name  First name  User ID  Phone Number Address Masterson  Mollissa    mm987601 804-444-0000 8446 Somewhere Dr. Peterson   William     pw987701 804-555-0000 6755 Park Ave. Dickens    Alexander   da435401 804-666-0000 4565 Nowhere Park Williamson Marrideth   wm937401 703-777-0000 4654 Overthere Dr. Thompson   Gerald      tg994401 804-888-0000 6221 Lonely Street Patterson  Martha      pm747401 703-999-0000 3345 Party Lane 

This file consists of 13 lines, each of which is 80 characters long. It stores information about company employees . All employee user IDs begin at column position 28 and end at column position 35. The following JScript code reads this file and extracts a list of user IDs.

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); var inputFile = fsoObject.OpenTextFile("c:\myDocs\ Employee.txt", 1, "True"); for (i=0; i<7; i++) {   inputFile.skipLine() } while (!inputFile.AtEndOfStream) {   inputFile.skip(27)   userID = inputFile.Read(8);   WScript.Echo(userID);   inputFile.skip(46) } inputFile.Close(); 

The first thing that happens in this script is that the FileSystemObject is instantiated . Then an object reference is set up for the input file. Next, a for loop is set up that uses the SkipLine() method to skip the processing of the first 7 lines in the input file. A while loop is set up that extracts the user ID of each employee using the Skip() method to skip the first 27 characters on each line, the Read() method to extract the next 8 characters, and the Skip() method to skip the rest of the characters on that line of the file, thus moving the file pointer down to the first column of the next row in the file. The while loop runs until inputFile.AtEndOfStream is true, which occurs when the last line in the file has been processed .

If you were to save and run this example using the CScript execution host, you would see the outline shown in Figure 6.12.

Figure 6.12. Using the Read() method to process data stored in a formatted file


Processing the Contents of an Entire File

In addition to reading an input file a line at a time or a few characters at a time, you also have the option of reading the entire input file in a single read operation using the FileSystemObject object's ReadAll() method. For example, the following JScript code displays the entire contents of c:\MyDocs\Sample.txt , using the WScript object's Echo() method.

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); var inputFile = fsoObject.OpenTextFile("c:\MyDocs\Sample.txt", 1, "True");  reportFile = inputFile.ReadAll(); inputFile.Close(); WScript.Echo(reportFile); 

When run, this example uses the ReadAll() method to store all the data in the input file in a variable called reportFile . The input file is then closed, and the file's contents are displayed, as shown in Figure 6.13.

Figure 6.13. Using the ReadAll() method to process all the content of a file in one operation


[ LiB ]

Learn JavaScript In a Weekend
Learn JavaScript In a Weekend, Second Edition
ISBN: 159200086X
EAN: 2147483647
Year: 2003
Pages: 84 © 2008-2017.
If you may any questions please contact us: