I'm trying to create a button which deletes the files/folders to which selected shortcuts point to, and also delete the shortcuts themselves. The shortcuts are always on the Desktop.
I have the following code where the shortcut target is being deleted, but the shortcut itself fails to be deleted.
The problem is that the @resolvelinks somehow interferes with the file$ of the last Delete command (the command works without that modifier), but IMO it shouldn't have any such side effects for just the file$.
function OnClick(clickData)
{
var cmd = clickData.func.command;
cmd.ClearFiles();
var wsh = new ActiveXObject("WScript.Shell");
var anyItems = false;
for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext())
{
var item = eSel.item();
if (!item.is_dir && item.ext.toUpperCase() == ".LNK")
{
var lnk = wsh.CreateShortcut(item.realpath);
var target = lnk.TargetPath;
cmd.AddFile(item);
if (target != "")
{
cmd.AddFile(target);
}
anyItems = true;
}
}
if (anyItems)
{
cmd.RunCommand("Delete");
}
}
If you also want it to delete non-link files, and folders, which are directly selected, change it to this:
function OnClick(clickData)
{
var cmd = clickData.func.command;
cmd.ClearFiles();
var wsh = new ActiveXObject("WScript.Shell");
var anyItems = false;
for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext())
{
var item = eSel.item();
cmd.AddFile(item);
anyItems = true;
if (!item.is_dir && item.ext.toUpperCase() == ".LNK")
{
var lnk = wsh.CreateShortcut(item.realpath);
var target = lnk.TargetPath;
cmd.AddFile(item);
if (target != "")
{
cmd.AddFile(target);
}
}
}
if (anyItems)
{
cmd.RunCommand("Delete");
}
}
Add the QUIET arg to the delete command at the bottom if you still want that, but you'll only get one confirmation prompt for everything (summarising how many files and folders will be deleted), so you might want to keep it.
Ah true, it didn't cross my mind, some shortcuts indeed don't have exactly the same name as their targets: some are for example program.lnk and some program.exe.lnk depending on how they were created. Your script nicely solves all such cases.