Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBScript, JScript, Wscript ... oh my

I need to write some scripts for WinXP to support some of the analysts here at Big Financial Corp. Please help me decide which type of windows scripting best fits my needs.

My needs seem pretty simple (to me anyway)

  1. run on WinXP Pro SP2 (version 2002)
  2. not require my users to install anything (so Powershell is out. Likewise Perl, Python, and other common suggestions for these types of questions on stackoverflow)
  3. written in a non-compiled language (so users have a chance to modify them in the future)
  4. reasonably complete language features (especially date/time manipulation functions. I would like to also have modern concepts like subroutines, recursion, etc)
  5. ability to launch and control other programs (at the commandline)

From my hurried review of my options, it looks like my choices are

  1. VBScript
  2. WScript
  3. JScript

I don't have time to learn or do an in-depth review of these (or whatever else a standard install of WinXP has available). I have a pretty urgent need to pick on and hack something together as quickly as possible.

(Current crisis is the need to run a given application, passing several date parameters).

Once the current crisis is over, there will be more requests like this.

Help me Obi Wan Stackoverflow... you're my only hope.

[edit] My current skill set includes Perl, Javascript, and Java so I'm most comfortable using something similar to these

[edit] ok. I'll try writing a WSH file in JScript. Thanks everyone... I'll let you know how it goes (and figure out accepting an answer) once things settle down around here a bit.

[edit] It all worked out in the end. Thanks for the quick responses folks. Here's what I gave my user

<job id="main">
    <script language="JScript">
// ----- Do not change anything above this line ----- //

var template = "c:\\path\\to\\program -##PARAM## --start ##date1## --end ##date2## --output F:\\path\\to\\whereever\\ouput_file_##date1##.mdb";

// Handle dates
// first, figure out what they should be
dt = new Date();
var date1 = stringFromDate(dt, 1);
var date2 = stringFromDate(dt, 2);

// then insert them into the template
template = template.replace(new RegExp("##date1##", "g"), date1);
template = template.replace(new RegExp("##date2##", "g"), date2);

// This application needs to run twice, the only difference is a single parameter
var params = ["r", "i"]; // here are the params.

// set up a shell object to run the command for us
var shellObj = new ActiveXObject("WScript.Shell");

// now run the program once for each of the above parameters
for ( var index in params )
{
    var runString = template; // set up the string we'll pass to the wondows console
    runString = runString.replace(new RegExp("##PARAM##", "g"), params[index]); // replace the parameter
    WScript.Echo(runString);

    var execObj = shellObj.Exec( runString ); 
    while( execObj.Status == 0 )
    {
        WScript.Sleep(1000); //time in milliseconds
    }
    WScript.Echo("Finished with status: " + execObj.Status + "\n");
}


// ----- supporting functions ----- //

// Given a date, return a string of that date in the format yyyy-m-d
// If given an offset, it first adjusts the date by that number of days
function stringFromDate(dateObj, offsetDays){
    if (typeof(offsetDays) == "undefined"){
        offsetDays = 0;
    }
    dateObj.setDate( dateObj.getDate() + offsetDays );

    var s = dateObj.getYear() + "-";     //Year
    s += (dateObj.getMonth() + 1) + "-"; //Month (zero-based)
    s += dateObj.getDate();              //Day

    return(s);
}

// ----- Do not change anything below this line ----- //
    </script>
</job>

Clearly it could be better... but it got the job done and is easy enough for my user to understand and extend himself.

like image 326
Sukotto Avatar asked Oct 02 '09 19:10

Sukotto


2 Answers

These are all technically the same thing with different syntax. Actually WScript/CScript is the engine, VBScript and JScript are the languages.

Personal opinion only follows: My personal recommendation is JScript because it reminds me more of a real programming language, and makes me want to punch myself in the face less often than VBScript. And given your familiarity with javascript, your best bet is JScript.

Going into a bit more detail about the difference between WScript and CScript as others have: these are your execution platforms for your scripts for the Windows Script Host. They are essentially the same thing, whereas WScript is more GUI oriented, and CScript is more console oriented. If you start the script with CScript, you will see a console window, but you still have access to GUI functionality, whereas if you start with WScript, there is no console window, and many of the default output methods display as windowed objects rather than a line in the console.

like image 61
snicker Avatar answered Sep 30 '22 17:09

snicker


If you like JavaScript, you'll probably be ok with JScript. It's a decent language, and certainly more suitable for complex scripts than VBScript.

However, Microsoft1hates JavaScript, so you'll encounter some APIs that are trivial to use with VBScript but painful to access using JScript. Consider yourself warned...

As snicker notes, WScript is the engine that drives both.

1 Anthropomorphization used to note general lack-luster support; not to be interpreted as evidence of any official policy.

like image 20
Shog9 Avatar answered Sep 30 '22 17:09

Shog9