Listing 1: The main Function function main() { var args, options, output, fso, logappend, dirs, dir, folder; args = WScript.Arguments; if ((args.Unnamed.length == 0) || (args.Named.Exists("?"))) usage(); if (scripthost() != "cscript.exe") { WScript.Echo("You must run this script using the CScript host"); return 1; } // ******* BEGIN CALLOUT A ******* // This object contains the parameters and returned values // for the delete operations. options = { dayslimit: 0, cutoffdate: null, sizelimit: 0, recurse: false, testmode: false, logging: false, logfn: null, logts: null, filetotal: 0, bytetotal: 0 }; // ******* END CALLOUT A ******* // If /d exists, parse its argument and verify it's greater than zero. if (args.Named("d") != undefined) { options.dayslimit = parseInt(args.Named("d")); if (isNaN(options.dayslimit) || (options.dayslimit <= 0)) { WScript.Echo("! Error: Days must be a number > 0"); return ERROR_INVALID_PARAMETER; } options.cutoffdate = datefromdaysago(options.dayslimit); } // If dayslimit is zero (i.e., the user hasn't specified /d) and if /z // exists, parse the /z argument and verify it's greater than zero. if ((options.dayslimit == 0) && (args.Named("z") != undefined)) { options.sizelimit = parseInt(args.Named("z")); if (isNaN(options.sizelimit) || (options.sizelimit <= 0)) { WScript.Echo("! Error: Size must be a number > 0"); return ERROR_INVALID_PARAMETER; } } // The user must specify either /d or /z. if ((options.dayslimit == 0) && (options.sizelimit == 0)) usage(); // Check for /s and /t. options.recurse = args.Named.Exists("s"); options.testmode = args.Named.Exists("t"); fso = new ActiveXObject("Scripting.FileSystemObject"); // ******* BEGIN CALLOUT B ******* // By default, enable logging only if /l exists on the // command line and don't append to the log file. logappend = false; options.logging = args.Named.Exists("l"); // Check to see whether /l exists on the command line. if (options.logging) { // Read the /l parameter's argument. options.logfn = args.Named("l"); // Check to see whether the argument is a non-zero length string. if ((typeof options.logfn == "string") && (options.logfn.length > 0)) { // Append if the first character is a +. logappend = options.logfn.charAt(0) == "+"; if (logappend) { // The filename is the remainder of the argument following +. options.logfn = options.logfn.substr(1); // If only + was specified, use the default filename. if (options.logfn == "") options.logfn = fso.BuildPath(scriptpath(), DEFAULT_LOGFILE); } } else options.logfn = fso.BuildPath(scriptpath(), DEFAULT_LOGFILE); try { options.logts = fso.OpenTextFile(options.logfn, logappend ? FOR_APPENDING : FOR_WRITING, true); } catch(err) { WScript.Echo("! Error 0x" + hex(err.number) + " opening " + options.logfn); options.logging = false; } } output = "> Operation: "; if (options.dayslimit > 0) output += "Delete files older than " + formatnumber(options.dayslimit) + " day(s) (" + formatdate(options.cutoffdate) + ")"; else if (options.sizelimit > 0) output += "Delete files to limit directory size(s) to " + formatnumber(options.sizelimit) + " megabyte(s)"; outputdata(output, options); // ******* END CALLOUT B ******* // Iterate the unnamed arguments on the command line. dirs = new Enumerator(args.Unnamed); for (; ! dirs.atEnd(); dirs.moveNext()) { dir = dirs.item(); try { folder = fso.GetFolder(dir); } catch(err) { outputdata("! Error 0x" + hex(err.number) + " reading " + dir, options); continue; } process(folder, options); } output = options.testmode ? "> Test mode: Found " : "> Finished: Deleted "; output += formatnumber(options.filetotal) + " file(s), " + formatnumber(options.bytetotal) + " byte(s)"; outputdata(output, options); // Close the log file. if (options.logging) options.logts.Close(); return 0; } 1