Creating Report and Log Files

[ LiB ]

Creating Report and Log Files

Once you have opened a file in either ForWriting or ForAppending mode, you can begin to write output to it. There are several ways you can do this. First of all, you can write output a few characters at a time. This often is a good technique to use when creating formatted reports that present data in rows and columns .

You also can write output a line at a time. This facilitates the creation of free-form reports and log files. For example, you might want to create a script log file to which you record all error messages generated by your scripts. Another way to write output to text files is to write blank lines. You may want to add blank lines to text files in order to improve readability and to offset specific sections of reports.

Writing Output a Few Characters at a Time

If you want, you can write a specific number of characters to an output file. This is done using the FileSystemObject object's Write() method. The Write() method writes whatever text you pass to the line in the output file where the file pointer is currently positioned. After writing the new text, the file pointer is advanced to the right, but a carriage return is not performed. Therefore, the next write operation will occur on the same line as the previous write operation, and its output will be appended to the end of the previous output.

To see how this works, take at look at the following example:

 var fsoObject = new ActiveXObject("Scripting. FileSystemObject"); var outputFile = fsoObject.OpenTextFile("c:\MyDocs\ Sample.txt", 2, "True"); outputFile.Write("Once upon a time there were "); outputFile.Write("three little bears."); outputFile.Close(); 

First the FileSystemObject object is instantiated . Then an object reference is set up for a file named Sample.txt . The file opens in ForWriting mode. The Write() method is then executed twice, and each time a different text string is written, after which the file is closed.

NOTE

Note that the previous example assumed that the folder c:\MyDocs already existed. If it did not exist when the script was run,an error would occur. Therefore,it is always a good idea to check and make sure that a folder exists before you attempt to access its contents. You can accomplish this using the FileSystemObject object's CreateFolder() method, which I will cover a little later.

Figure 6.8 shows how the text will look when written to the output file.

Figure 6.8. Creating a small text file using the FileSystem Object object's Write() method

graphic/06fig08.gif


Writing Output a Line at a Time

Usually, you'll want to write output to text files a line at a time. You can do this using the FileSystemObject object's WriteLine() method. For example, the following script uses the WriteLine() method in place of the Write() method used in the previous example:

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); var outputFile = fsoObject.OpenTextFile("c:\MyDocs\ Sample.txt", 2, "True"); outputFile.WriteLine("Once upon a time there were "); outputFile.WriteLine("three little bears."); outputFile.Close(); 

When this script runs, the output from the two WriteLine() methods is written to the output file on two different lines, as demonstrated in Figure 6.9.

Figure 6.9. Writing output to files a line at a time

graphic/06fig09.gif


Formatting Output Files with Blank Lines

You can add blank lines to your output files when generating reports in order to make them more attractive and easier to read. To do so, you will need to use the FileSystemObject object's WriteBlankLines() method. This method appends a blank line to the end of the output file and then performs a carriage return.

As a demonstration of how to use the WriteBlankLines() method, take a look at the following JScript:

 //************************************************************ ************* //Script Name: Script 6.5.js //Author: Jerry Ford //Created: 09/14/03 //Description: This JScript demonstrates how to create and //format a typical reports file. //************************************************************ ************* //Initialization Section  //Instantiate the FileSystemObject var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); //Set up an object reference for the output file var outputFile = fsoObject.OpenTextFile("c:\MyDocs\ Sample.txt", 2, "True"); //Set up an object reference for the computer's hard drive var hardDrive = fsoObject.GetDrive("c:") //Main Processing Section WriteToFile() CloseTheFile() //Terminate the script's execution WScript.Quit(); //Procedure Section function WriteToFile() {   outputFile.WriteBlankLines(1);   outputFile.WriteLine("====================================== ===========");   outputFile.WriteBlankLines(1);   outputFile.WriteLine(Date() + "     C: Drive Analysis:");   outputFile.WriteBlankLines(1);   outputFile.WriteLine("====================================== ==============");    outputFile.WriteBlankLines(1);   outputFile.WriteBlankLines(1);   outputFile.WriteLine("Serial No. \t\t" + hardDrive.SerialNumber);   outputFile.WriteBlankLines(1);   outputFile.WriteLine("Volume Name \t\t" + hardDrive.VolumeName);   outputFile.WriteBlankLines(1);   outputFile.WriteLine("Drive Type \t\t" + hardDrive.DriveType);   outputFile.WriteBlankLines(1);   outputFile.WriteLine("File System \t\t" + hardDrive.FileSystem);   outputFile.WriteBlankLines(1);   outputFile.WriteLine("Total Size  \t\t" + hardDrive.TotalSize);   outputFile.WriteBlankLines(1);   outputFile.WriteLine("Free Space \t\t" + hardDrive.FreeSpace);   outputFile.WriteBlankLines(1);   outputFile.WriteLine("====================================== =============="); } function CloseTheFile() {   outputFile.Close(); } 

This script begins by instantiating the FileSystemObject object. It then sets up object references to the script's output file using ForWriting mode and to the computer's C drive using the FileSystemObject object's GetDrive() method. Next the WriteToFile() function is called, which is responsible for writing the formatted report to the output file. The CloseTheFile() function is then called to close the output file, and the script then terminates its own execution.

NOTE

The WriteBlankLines() method also enables you to pass a number representing the number of blank lines you want to write.

Figure 6.10 shows the output that this script generated when it was run on my computer.

Figure 6.10. Adding blank lines to reports makes them easier to read.

graphic/06fig10.gif


[ LiB ]


Learn JavaScript In a Weekend
Learn JavaScript In a Weekend, Second Edition
ISBN: 159200086X
EAN: 2147483647
Year: 2003
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net