JScript Reference
class myClass {
var i : int = 42;
}
class yourClass {
var s : String = "Hello";
var f : float = 3.142;
var d : Date = new Date();
}
// Define a variable of each user-defined type.
var mine : myClass = new myClass();
var yours : yourClass;
// This fails because there is no obvious way to convert
// from myClass to yourClass
yours = yourClass(mine);
//jscript includes:
function include(file) {
var fso = new ActiveXObject('Scripting.FileSystemObject')
var fil = fso.opentextfile(file)
var buf = fil.readall()
fil.close()
return(buf)
}
eval(include('c:\\scripts\\functions1.js'))
eval(include('c:\\scripts\\functions2.js'))
not preferred: var = WScript.createobject('Scripting.FileSystemObject')
preferred: var = new ActiveXObject('Scripting.FileSystemObject')
In addition to files with .vbs or .js extensions, WSH 2.0 also recognizes
the .wsf extension. Files with the .wsf extension are scripts encoded in
XML format. The advantages of using XML encoded scripts include the ability
to use multiple scripting languages within the same script file, write
reusable code and specify references to external type libraries.
An interesting aspect of WSH is that it contains two platform-specific
executables, cscript.exe and wscript.exe. The echo (output) for cscript is
to a command window (via a Win32 console application). While the echo
(output) for wscript is to a graphical message box (via Windows). Just
remember that the c stands for console and the w for Windows. For a full
list of arguments, enter "cscript /?" or "wscript /?" at a command prompt.
How do I use long file names with WSH
Problem
I get strange behavior when trying reference long file names. For example
when I try to run a program using WScript.Shell, I get errors and the
application does not run.
var shell = WScript.CreateObject('WScript.Shell')
var rst = shell.Run('c:\\program files\\plus!\\microsoft internet\\iexplore.exe',1,true)
Solution
Wrap the path to the file in quotes. In JScript, you use the backslash
character to escape special characters. We want to put a set of double
quotes within an existing set of double quotes and need to escape the
quotes inside using \".
var shell = WScript.CreateObject('WScript.Shell')
var rst = shell.Run("\"c:\\program files\\plus!\\microsoft internet\\iexplore.exe\"",1,true)
fs = new ActiveXObject( "Scripting.FileSystemObject" );
fs.CreateTextFile( "firstfile.txt" );
os = fs.GetFile( "firstfile.txt" );
os = os.OpenAsTextStream(forAppending,0);
//forReading
//forWriting
//forAppending
//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.writeline("Goodbye World");
os.Close();
// Change to your folder you want the size of
var folder = "F:\\winnt";
// Create FSO and Grab the folder you defined
var fso = new ActiveXObject( "Scripting.FileSystemObject" );
var fl = fso.GetFolder( folder );
// Get the byte size of folder and sub folders. Format
// into MB as well.
var byteSize = fl.Size;
var mbSize = new String( byteSize / 1048576 ).substr( 0, 5 );
// Create display message
var msg = "The size of " + folder + " is\n";
msg += byteSize + " (bytes)\n";
msg += mbSize + " MB\n";
WScript.Echo( msg );
The script display a message box with a list
of all the files in the C:\\temp folder.
/* Create the FileSystemObject */
fso = new ActiveXObject("Scripting.FileSystemObject");
/* Point the Object to a specific folder */
fsofolder = fso.GetFolder("C:\\temp");
/* get a collection of the files contained in that folder */
colFiles = fsofolder.Files;
/* Create an Enumerator so that we can move through the collection */
fc = new Enumerator( colFiles );
/* Create a variable to store an output message in */
var msg = "";
/* Loop through the Enumerator and add each item to our variable */
for (; !fc.atEnd(); fc.moveNext() ){
msg += fc.item() + "\n";
}
/* Show the user the results */
WScript.Echo( msg );