views:

981

answers:

7

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.

+10  A: 

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.

snicker
+1 for avoiding punching self in the face.
outis
visual basic in any form drives me to self-inflicted harm. sometimes the auto-face-punch is involuntary
snicker
I also prefer JScript because knowing how to write Javascript is a more generally useful skill than writing VB. Even though I've used VB since version 1.0, I still favor JScript for this task, a) because I want to improve my Javascript understanding, and b) it sets a better example for others to follow. There is one process at my dayjob where there are a dozen batch files copying and renaming text files and calling short PERL scripts. The whole mess could have been written in a single clean documented JScript file replacing both the .BAT files and the amateurish PERL.
Michael Dillon
+1  A: 

My choice would be WSH using JScript. You could use VBScript, but why, when JScript is available.

Here is a reference for Windows Script Host.

RedFilter
+1  A: 

Don't forget CScript. And be careful here, because the windows scripting host is often disabled by group policy at large companies. If that's the case, the only option that fits all your criteria is (shudder) batch.

If none of those work out for you, your best option is probably a compiled program where you distribute the source with the program.

Joel Coehoorn
what is CScript? I don't think it is an active scripting language installed by default..
snicker
@snicker: not a language, just a language host designed for the console rather than the windowing system (contrast with WScript).
Shog9
I knew that, I just was pointing out that it's not a language.
snicker
Heh, sorry... :-)
Shog9
Hey, why shudder at batch files? Some of us have to use less popular topics for gathering rep :-þ
Joey
+1  A: 

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, Microsoft1 hates 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.

Shog9
A: 

Use JScript. A key difference between using JScript with the WScript/cscript engine and writing JavaScript in the browser is that you don't have the browser security restrictions. You also have access to ActiveX/COM objects for manipulating applications, the registry, etc. In fact, you'll probably spend a lot more time reading up on those custom objects and interfaces than worrying about the language features. Of course, you get all the benefits of JavaScript dates, regex's, etc.

A sample JScript app from MSDN might help to get you started.

Unfortunately, most of Microsoft's sample scripts tend to be VBScript, but the syntax is pretty easy to follow, especially if you're just trying to pick out COM interface details.

system PAUSE
+1  A: 

To expand on the other's answers a bit, WScript and CScript are programs used to run scripts written in VBScript (Visual Basic like syntax) or JScript (JavaScript-like syntax). CScript runs scripts from a console window, so that the Echo command writes to the console, whereas WScript runs them without a window and the Echo command writes to a popup message box.

You can write WSH (Windows Scripting Host) and WSC (Windows Scripting Component) scripts that use both VBScript and JScript by combining them in an XML-based wrapper, if you need to merge pre-existing code in the two languages.

You can also write HTA scripts, which stands for "HyperText Application". These are script code in an HTML file with the HTA extension that runs in Internet Explorer, which allows you to provide an interface using HTML controls, but also have complete access to your system because the run locally.

All of these are based on the Windows Scripting Host and Active Scripting technologies which have been included with all Windows computers since Windows 98. Along with fairly powerful base languages, they also give you access to WMI for extensive system and network information and management, and COM capability for automating Word, Excel etc. Also you can use ADO and ADOX to create and work with Access MDB files even if Access is not installed.

Todd
A: 

Although JScript is a much less horrible language than VB Script, the problem is that VB Script has a more complete library of helpful functions built into it for things like date and number formatting. So it's not actually as easy a choice as it first appears, unless you are able to write and install your own library of helper objects to use with JScript.

Some details here.

Daniel Earwicker