Primordial Win Script

Here is one tasty little script from the pre-power shell era. Is it useful? I certainly hope so. Will it run on your W7 ? Certainly it will. So … what is this script for?


wshI have a lot of development artifacts on all my machines. This is one barely manageable ocean, of files and folders from last decades of computing. So I tend to create several substituted drives where I always keep the same kind of stuff, and where I always do the same kind of things.

For example I always develop on the (famous) X: drive. All my IDE setups, batch files and scripts, assume they are living on the drive with the same root: “X:”.

This is especially useful for team work. VSS is notorious for confusing everyone and everything. If the whole team has working folders on the same drive (yes, the X:), on each and every machine, then managing them is much more erm… manageable. Then , here we also have the backup tools and scripts, building environments etc. All in all we always need and have several important drive letters on all development workstations, and servers too.

To make that kind of a WIN desktop more manageable, I have developed this simple but mission critical script.

If no arguments are given, it simply creates X: subst drive, with the X: root mapped to wherever the script is sitting. Optionally it can receive a single argument telling it what the new drive letter should be. On any illegal substitution attempt it will just ignore you. At the end it will pop up the file explorer with the new drive as the root of the tree view. Neat.

Here is the script:

<package>
<comment>
MIT Style Copyright© 2001-2010 by DBJ@DBJ.ORG. All Rights Reserved.
NOTE : This script creates new drive by substitution.
Default drive letter is "X:"
Otherwise a first argument to this script will define the
new drive letter.
Subst-ed drive root will be the folder from which this script is
started.
Therefore. If starting this script from a shortcut
be sure to check the "Start In:" value from its shortcut
properties dialogue. Set it to something else if you do
not want the default behaviour.
</comment>
<job id="open_explorer_with_required_root">
<!-- if debug="false" the 'debugger;' statement[s]
will be silently ignored -->
<?job error="true" debug="false" ?>
<!---->
<reference object="WScript.Shell"></reference>
<object id=shell progid="WScript.Shell" ></object>
<!----->
<script language="JScript" id="lib">
//@cc_on
//@set @DEBUG = true
debugger;
//------
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, "");
};
//------
function print( s_, err ) {
var longline = "\n................." ;
var rez = "" ;
if (s_ instanceof Error)
{
// this is a run-time error from the script engine
rez = "\nERROR:\n" + s_.name + " " ;
rez += "\nNumber : " + (s_.number &amp; 0xFFFF) ;
rez += "\nDescription : " + s_.description + "";
} else {
err = false ;
rez = s_ ;
}
//@if (@DEBUG == true)
shell.Popup( s_ + longline , 5 ) ;
//@end
if ( err ) shell.LogEvent(1,s_) ;
}
</script>
<script language="JScript" id="main">
//------
main( ) ;
//------
// return new drive letter
// X: if none given as argument
function new_drive_letter ( def_ltr, x)
{
def_ltr = def_ltr == null ? "X:" : def_ltr ;
var script_args = WScript.Arguments ;
if ( script_args.length < 1 ) return def_ltr ;
try {
var legal = "DEFGHIJKLMOPQURSTVWXYZ" ;
var retval = script_args(0).trim().charAt(0).toUpperCase() ;
if ( legal.indexOf(retval) < 0 ) return def_ltr ;
return retval.toUpperCase() + ":" ;
} catch(x){
print ( x ) ;
return def_ltr ;
}
}
//------
function main( x )
{
var new_ltr = new_drive_letter() ;
var current_dir = shell.CurrentDirectory ;
//@if (@DEBUG == true)
function dump_args ()
{
var rezult = "Command Line Arguments Dump" ;
rezult += "\n---------------------------------" ;
rezult += "\nlength = " + WScript.Arguments.length ;
var objArgs = WScript.Arguments;
for (i = 0; i < objArgs.length; i++)
{
rezult += ( "\ncli arg[" + i + "] = " + objArgs(i) );
}
rezult += "\n---------------------------------" ;
rezult += "\nNew Drive Letter = " + new_ltr ;
rezult += "\n---------------------------------" ;
rezult += "\nCurrent directory = " + current_dir ;
print( rezult ) ;
}
//@end
try {
//@if (@DEBUG == true)
dump_args() ;
//@end
var errno = 0 ;
// we shell subst the drive, if not already subst-ed
try {
errno = shell.Run('subst ' + new_ltr + ' "' + current_dir + '"',2,true ) ;
} catch ( x ) { print(x) ;
}
// Before we run the explorer to have as the tree view root the newly subst
// drive we MUST sleep for a while. Otherwise the system
// will not be able to
// display the newly subst drive
if ( errno == 0 ){
WScript.Sleep(5000);
}
shell.Run("explorer /e,/root," + new_ltr ) ;
} catch( x ) {
print(x) ;
}
CollectGarbage() ;
}
/////
</script>
</job>
<!------->
</package>

Yes this is an WSF file. Just create on your machine one empty <whatever>.wsf file, and then paste this script into it.
If you find this script usefull please feel free to “jump” into the MSDN ocean, in case you are really curious about WSF , this “funny” markup, etc …