Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Created October 29, 2008 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifaziz/20716 to your computer and use it in GitHub Desktop.
Save atifaziz/20716 to your computer and use it in GitHub Desktop.
Recyclable Delete Utility (RDEL)
//
// Recyclable Delete Utility (RDEL)
// Written by Atif Aziz
// http://www.raboof.com/
//
// Copyright (c) 2004-8 Atif Aziz. All rights reserved.
//
// DISCLAIMER: The software and source code is provided "AS IS" with no
// warranties of any kind. The entire risk arising out of the use or
// performance of the software and source code is yours.
//
var stdout = WScript.StdOut;
var stderr = WScript.StdErr;
var DELETE_VERB = "delete";
var ExitCode =
{
MISSING_ARG : -1,
INVALID_PATH : -2,
INVALID_FOLDER_ITEM : -3,
VERB_FAILED : -4,
CANNOT_DELETE : -5
};
if (0 == WScript.Arguments.length)
exit(ExitCode.MISSING_ARG, "Missing file or folder name.");
//
// Get the target item to delete. This must be the first
// argument to this script.
//
var targetPath = WScript.Arguments(0);
//
// Split the path and file name of the target. If the
// path is missing then use the current directory as
// the path.
//
var path;
var fileName;
var nameSparatorIndex = targetPath.lastIndexOf("\\");
if (nameSparatorIndex < 0)
{
var shell = new ActiveXObject("WScript.Shell");
path = shell.CurrentDirectory;
fileName = targetPath;
}
else
{
path = targetPath.substring(0, nameSparatorIndex);
fileName = targetPath.substring(nameSparatorIndex + 1);
}
//
// Get the shell folder representing the target's path from
// in the shell namespace.
//
var shell = new ActiveXObject("Shell.Application");
var folder = shell.NameSpace(path);
if (!folder)
exit(ExitCode.INVALID_PATH, "'" + path + "' is not a valid path.");
//
// Get the target item from the folder.
//
var folderItem = folder.ParseName(fileName);
if (!folderItem)
exit(ExitCode.INVALID_FOLDER_ITEM, "'" + fileName + "' is not a valid file or folder.");
//
// Find the delete verb for the shell folder item.
//
var deleteVerb = null;
for (var e = new Enumerator(folderItem.Verbs());
!e.atEnd(); e.moveNext())
{
var verb = e.item();
var displayName = verb.Name.replace(/&/, ''); // Remove the short-cut indicator, the ampersand (&)
if (displayName.toLowerCase() == DELETE_VERB)
{
deleteVerb = verb;
break;
}
}
if (!deleteVerb)
exit(ExitCode.CANNOT_DELETE, "'" + targetPath + "' cannot be deleted.");
//
// Invoke the deletion verb!
//
try
{
deleteVerb.DoIt();
}
catch (e)
{
//
// If the user cancels and decides not to delete then DoIt
// throws an exception so the error code for it needs
// be checked here explicitly and ignored.
//
// 0x800704c7 = The operation was canceled by the user.
//
if (e.number != 0x800704c7)
exit(ExitCode.VERB_FAILED, e.description);
}
//
// Halts the script, setting an exit code and optionally printing
// a message to the standard error stream.
// NOTE! This function does not return control.
//
function exit(code, msg)
{
if (msg && msg.toString().length > 0)
stderr.WriteLine(msg);
WScript.Quit(code);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment