Thursday, March 1, 2012

The Best Console Tool for Windows is JavaScript (tips)

You can use JS with your installation scripts, and with your just do it utilities.  Or for a better quick scripting in windows, JavaScript rules!

Executing

cscript file.js param1 param2

Get Console Parameters

WScript.Arguments.Item(0); // retrieves param1

Get Environmental variables

var WshShell = new ActiveXObject("WScript.Shell");

var oEnv = WshShell.Environment("Process");

// the %PATH% environmental variable
var path = oEnv("PATH");


var print = function(msg) {
    WScript.Echo(msg);
};

var getenv = function(n) {
    return oEnv(n);
};


printing to console

WScript.Echo(msg);


reading a text file

var ofso = new ActiveXObject("Scripting.FileSystemObject")
// read-only
var ofile = ofso.OpenTextFile(filepath, 1);
var sContents = ofile.ReadAll();
ofile.Close();