Winscripter
  WSH
  Forums
  Downloads
  Books
  Links
  Amazon





© winscripter.com
1998-2004







Howto: Read a Text File

Posted by on Sunday, January 18, 2004 (PST)

Howto: Read a text file, load each line into and Array, and print each line out.
Uses Scripting.FileSystemObject GetFile Method.

 

// define constants
// Note: if a file exists, using forWriting will set
// the contents of the file to zero before writing to
// it.

var forReading = 1, forWriting = 2, forAppending = 8;
// define array to store lines. 
rline = new Array();
// Create the object 
fs = new ActiveXObject("Scripting.FileSystemObject");
f = fs.GetFile("test.txt");
// Open the file 
is = f.OpenAsTextStream( forReading, 0 );
// start and continue to read until we hit
// the end of the file.

var count = 0;
while( !is.AtEndOfStream ){
   rline[count] = is.ReadLine();
   count++;
}
// Close the stream 
is.Close();
// Place the contents of the array into 
// a variable.
var msg = "";
for(i = 0; i < rline.length; i++){
   msg += rline[i] + "\n";
}
// Give the users something to talk about. 
 
WScript.Echo( msg );

 

Details 

var forReading = 1, forWriting = 2, forAppending = 8; 

Sets the constants for the "OpenAsTextStream" method.

 

rline = new Array();

Creates an array to hold each line of the file. You do not need to store each line into an array, you could store the data directly into a variable. I use this method because a majority of the files that I read are line sensitive, such as log files.

 

fs = new ActiveXObject("Scripting.FileSystemObject");
f = fs.GetFile("test.txt");

These two lines create the FileSystemObject necessary to access a file and set a pointer to the desired file. You will probably want to retrive the name of the file at runtime using the CommonDialog Method I cover on this site or through a command line parameter.

 

is = f.OpenAsTextStream( forReading, 0 ); 

Open the file forReading. The two parameters for OpenAsTextStream([iomode,[format]]) are covered in the MS JScript Documentation, located at Microsoft's Scripting Site.
forReading    -> Opens the file for reading

0                -> Sets the format to ASCII

 

var count = 0;
while( !is.AtEndOfStream )
{
rline[count] = is.ReadLine();
count++;
}

Creates a variable to store the number of lines and to help out file the array indexing. Each line of the source file is read using the method "ReadLine()" and placed into the array. The count is incremented.

is.Close();

Closes the input stream. This will happen automatically when the script exits, but you should always cleanup after yourself.

 

var msg = "";
for ( i = 0; i < rline.length; i++ )
{
    msg += rline[i] + "\n";
}

A variable is created to store the contents of the array. Loop through the array from beginning to end and add each array element to the variable. Notice the new line "\n" added to each of the array elements. This is needed since it is stripped during the "ReadLine()" operation.

WScript.Echo( msg ); 

Display the contents of the file to the user. This method of displaying data to the user is quick, but it should not be used to display large amounts of data since the popup does not check for the size of the screen and can easily exceed the resolution of the monitor.

 




WSH and ADSI Administrative Scripting


Winscripter   |  WSH   |  Forums   |  Downloads   |  Books   |  Links   |  Amazon