Software Engineer
Restauranteur
Photographer
Cynic

23rd May 2012

Post

Windows Script Host

I’ve heard so little about WSH since it first came out, I actually had to verify it hadn’t been deprecated. Admittedly, batch files and shell scripts aren’t the sexiest topic, but they can be useful and save time for repetitive tasks. You can do things such as mapping drives, accessing special folders, manipulating the registry, and creating shortcuts. Here’s a link to Microsoft’s documentation.

Here’s a quick-and-dirty example I used to create desktop and start menu shortcuts depending on network access. I had two versions of this, one for end users to access production executables, and one for developers to access various development tools. Thus, for new hires, all we had to do was email them a link to the script and they got everything in one shot.

// create the shell object
Shell = new ActiveXObject("WScript.Shell");
fso = new ActiveXObject("Scripting.FileSystemObject");

// a series of checks for different EXEs, creating a shortcut on the desktop and Programs menu
if (fso.FileExists("S:\\MIILIFE\\mlife.exe")) {
 CreateVFPShortCut(Shell.SpecialFolders("Desktop"),"SelectAccount","S:\\MIILIFE","mlife.exe");
 CreateVFPShortCut(Shell.SpecialFolders("AllUsersPrograms"),"SelectAccount","S:\\MIILIFE","mlife.exe");
}
if (fso.FileExists("S:\\MIILTC2\\ltcproj.exe")) {
 CreateVFPShortCut(Shell.SpecialFolders("Desktop"),"LTC","S:\\MIILTC2","ltcproj.exe");
 CreateVFPShortCut(Shell.SpecialFolders("AllUsersPrograms"),"LTC","S:\\MIILTC2","ltcproj.exe");
}
if (fso.FileExists("S:\\MIIPROP\\miiprop.exe")) {
CreateVFPShortCut(Shell.SpecialFolders("Desktop"),"Proposals","S:\\MIIPROP","miiprop.exe");
 CreateVFPShortCut(Shell.SpecialFolders("AllUsersPrograms"),"Proposals","S:\\MIIPROP","miiprop.exe");
}

// Create shortcut code function
CreateVFPShortCut(Dir, Name, Path, Exe) {
link = Shell.CreateShortcut(Dir + "\\"+Name+".lnk");
link.Arguments = "";
link.Description = "Opens "+Name;
link.TargetPath = Path+"\\"+Exe;
link.WindowStyle = 3;
link.WorkingDirectory = Path;
link.Save();
}

Tagged: WSHWindows Script HostJScriptBatch Filesprogrammingshell scripting