The Tao of Jace

January 8, 2009

Copying Files Without Overwrite in JScript

Filed under: Archive — site admin @ 4:44 pm

Yeah, I know. JScript is the bane of my existence. It’s really a cool scripting language, but NO ONE USES IT. I’ve spent days trying to convert some code from VBScript to JScript because no one cared enough to write it out in the first place. Today I’ve written a function that allows you to copy the contents of one folder to another without overwriting existing files of the same name at the destination. I’m having to install some old software on a few user’s computers but all they really need are the OCX and DLL files, so we got those put into a common folder. Since these files are old (not touched since at least 1998), we don’t want to overwrite newer files of the same name. There’s no batch command that I can find that will do it, so I wrote a script that looks through the files of the source and compares them one by one to the destination, copying unmatched files to the destination. Here’s the code:

function CopyFiles(SourceFolder, TargetFolder, Overwrite) {
	var objFSO = WScript.CreateObject("Scripting.FileSystemObject");
	var count = 0;

	// handle optional variables
	if(Overwrite == null) {
		Overwrite = false;
	}

	if(SourceFolder.substr(SourceFolder.length - 1, 1) != "\\") {
		SourceFolder = SourceFolder + "\\";
	}
	if(TargetFolder.substr(TargetFolder.length - 1, 1) != "\\") {
		TargetFolder = TargetFolder + "\\";
	}

	if(!objFSO.FolderExists(TargetFolder)) {
		objFSO.CreateFolder(TargetFolder);
	}

	var FileNames = new Enumerator(objFSO.GetFolder(SourceFolder).Files);
	for(;!FileNames.atEnd();FileNames.moveNext()) {
		// check to see if the current file exists at destination
		if((objFSO.FileExists(TargetFolder + FileNames.item().Name) && Overwrite) || !objFSO.FileExists(TargetFolder + FileNames.item().Name)) {
			objFSO.CopyFile(SourceFolder + FileNames.item().Name, TargetFolder + FileNames.item().Name);
			count++;
		}
	}

	return count;
}

Now, it does allow overwriting if you need it to, but it assumes you don’t. Also it doesn’t handle subfolders. It wouldn’t be too hard to add a for loop that uses the Subfolders collection from the Folder object and then call the function recursively, but I don’t need to do that. Hope this helps someone out there.

January 7, 2009

JScript to Map Network Drive

Filed under: Archive — site admin @ 5:32 pm

So, more JScripting… This time I need to map a network drive for a program I’m moving between servers. Currently we’ve got people using a batch file or manually mapping the drive, but on the new server we’ll be applying a script that will handle this through group policy. I figure there’s a need for me to eventually map more than one drive and to also make sure there’s not anything already mapped to another location on the same drive. So I wrote a reusable script that checks through all the user’s mapped drives to see if the one we want is already taken and asks the user if it should reclaim the drive.

// MAP-DRIVE.js - Map Network Drive using provided specifications
// Author - Jace Gregg
// Version 1.0 - 07 January 2009
// -----------------------------------------------------------------'

// Just edit these fields:
//
var cTITLE = "Drive Mapping Script";
var mapDrive = "Z:";
var mapLocation = "\\\\path\\to\\server";
//
// No need to edit past here...

try {
	var shouldMap = null;
	var WshNetwork = WScript.CreateObject("WScript.Network");
	var oDrives = WshNetwork.EnumNetworkDrives();

	for(var i = 0; i < oDrives.length; i += 2) {
		if(oDrives.Item(i)==mapDrive) {
			if(oDrives.Item(i + 1).toLowerCase() != mapLocation) {
				// Drive is mapped to another location.  Ask the user if we should map to this location.
				shouldMap = msgbox("Drive " + mapDrive + " is mapped to \"" + oDrives.Item(i + 1).toLowerCase() + "\" but needs to be 

mapped to \"" + mapLocation + "\".  Do you want to automatically correct this?", null, null, 4 + 32);
			}
			else {
				shouldMap = false;
			}
		}
	}

	if(shouldMap == null) {
		shouldMap = true;
	}

	if(shouldMap) {
		WshNetwork.MapNetworkDrive(mapDrive, mapLocation);
	}
}
catch(e) {
	msgbox("Error " +  e.number + "\n\n" + e.description + "\nCould not connect network drive for this application.", null, null, 16);
}

function msgbox(message, title, popupTime, nType) {
	if(title == null) {
		title = cTITLE;
	}
	if(popupTime == null) {
		popupTime = 10;
	}
	var objWSH = WScript.CreateObject("Wscript.Shell");
	return objWSH.Popup(message, popupTime, title, nType);
}

You could add the ParseEnvironmentStrings method from my previous post and map to user directories or other various locations. Sure it’s not as efficient as it could be, but it does the trick. Feel free to change it to make it more efficient and use it however you want.

Powered by WordPress