Along with reading files, you will need to know how to create files. Here is a simple
example that walks you through the basics of file creation.
// 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;
// Create FileSystemObject
fs = new ActiveXObject( "Scripting.FileSystemObject" );
// Create the text file
fs.CreateTextFile( "firstfile.txt" );
// Open the file for appending
os = fs.GetFile( "firstfile.txt" );
os = os.OpenAsTextStream( forAppending, 0 );
// Write two lines of text to the file
os.write( "Hello World\r\n" );
os.write( "Goodbye World\r\n" );
// Close the file
os.Close();
var forReading = 1, forWriting = 2, forAppending = 8;
Standard file mode variables are created for easy reading of script.
fs = new ActiveXObject( "Scripting.FileSystemObject" );
In order to work with files we need to create a FileSystemObject.
Note: You can use CreateObject instead of using
new ActiveXObject. However, a note from Microsoft technical staff indicated
that using the latter involved less overhead for the system and should be considered the preferred
method of instantiating objects.
fs.CreateTextFile( "firstfile.txt" );
To create a file, we will call the CreateTextFile method of the FileSystemObject that
we just created.
os = fs.GetFile( "firstfile.txt" );
os = os.OpenAsTextStream( forAppending, 0 );
Now that the file has been created, we will create another object use to hold the specific file
and the mode with which we intend to use the file.
The OpenAsTextStream method takes two parameters.
The first is the file mode, which can contain forReading, forWriting, and forAppending (1, 2, 8) respectively.
- forReading (1) Opens the file for reading only.
- forWriting (2) Opens the file for writing. If the file exists the size is set to zero before writing.
in other words, it will completely destroy any content already contained in the file.
- forAppending (8) Opens the file for appending. All data written to the file will be appending to the end
of the file.
The second parameter contains the format with which to use;
TristateUseDefault, TristateTrue, TristateFalse (-2, -1, 0) respectively.
- TristateUseDefault (-2) Opens the file using the system default.
- TristateTrue (-1) Open the file as Unicode.
- TristateFalse (0) Open the file as ASCII.
os.write( "Hello World\r\n" );
os.write( "Goodbye World\r\n" );
Two lines of text are written to the file by calling the write method.
Note: The use of \r\n places a return and a new line
at the end of each line written to the file. Often, you will only see the \n which
will work in most situations. Additionally, you can use the WriteLine method to automatically
add the new line charactors to the end of each line.
os.Close();
Finally, we close the file stream. This will happen automatically when the script exits, but
closing the file through code is good programming practice.
Copyright 1999 Daren Thiel - www.winscripter.com