Discussion:
Send file to recycle bin in batch file or script (PowerShell?)
(too old to reply)
RobRSD
2007-10-07 23:14:00 UTC
Permalink
I would like a way to send a file to the Recycle Bin in a batch file or
script. I use the DEL command in .bat files to delete a file now. I would
much rather send it to the Recycle Bin.

Is this possible?

TIA,

Rob
Jim Barry
2007-10-08 12:30:39 UTC
Permalink
Post by RobRSD
I would like a way to send a file to the Recycle Bin in a batch file
or script. I use the DEL command in .bat files to delete a file now.
I would much rather send it to the Recycle Bin.
You can send a file to the recycle bin in script by invoking the verb "delete" (e.g. by calling IShellDispatch2.ShellExecute or FolderItem.InvokeVerb), but I don't see any way to prevent the "are you sure" confirmation from popping up, short of disabling it globally.
--
Jim Barry, MVP (Windows SDK)
RobRSD
2007-11-13 01:37:00 UTC
Permalink
I did not see Jim's post until now - thanks Jim! Thought I had notification
set.

I am pretty naive when it comes to scripting, and his syntax looks a little
object-oriented. What language is the post referring to? VBScript?
PowerShell?

TIA,

Rob
Post by Jim Barry
Post by RobRSD
I would like a way to send a file to the Recycle Bin in a batch file
or script. I use the DEL command in .bat files to delete a file now.
I would much rather send it to the Recycle Bin.
You can send a file to the recycle bin in script by invoking the verb "delete" (e.g. by calling IShellDispatch2.ShellExecute or FolderItem.InvokeVerb), but I don't see any way to prevent the "are you sure" confirmation from popping up, short of disabling it globally.
--
Jim Barry, MVP (Windows SDK)
Jim Barry
2007-11-13 19:56:36 UTC
Permalink
Post by RobRSD
I am pretty naive when it comes to scripting, and his syntax looks a
little object-oriented. What language is the post referring to?
VBScript? PowerShell?
Any script language that can use the shell automation object. Examples for JScript and VBScript are given here:

http://msdn2.microsoft.com/en-us/library/bb787816.aspx

So in JScript you can do this:

main();

function main()
{
var Shell = WScript.CreateObject("Shell.Application");
var Folder = Shell.Namespace("<path to file>");
var Item = Folder.ParseName("<name of file>");
Item.InvokeVerb("delete");
}

Or you can pass the full path directly to the desktop folder:

var Item = Shell.Namespace(0).ParseName("<path>");
--
Jim Barry, MVP (Windows SDK)
RobRSD
2007-11-13 21:14:02 UTC
Permalink
Thanks Jim for the examples and reference! Now I need to decide on the best
platform and if I want to replace the batch file or write a script that I can
use in place of the DEL command... If I replaced the DEL command, I would
want to be able to recongize a parameter... So, it appears that some
homework is in order. I have been thinking about getting into PowerShell
anyway.

Thanks again,

Rob
Post by Jim Barry
Post by RobRSD
I am pretty naive when it comes to scripting, and his syntax looks a
little object-oriented. What language is the post referring to?
VBScript? PowerShell?
http://msdn2.microsoft.com/en-us/library/bb787816.aspx
main();
function main()
{
var Shell = WScript.CreateObject("Shell.Application");
var Folder = Shell.Namespace("<path to file>");
var Item = Folder.ParseName("<name of file>");
Item.InvokeVerb("delete");
}
var Item = Shell.Namespace(0).ParseName("<path>");
--
Jim Barry, MVP (Windows SDK)
Jim Barry
2007-11-14 10:22:08 UTC
Permalink
Post by RobRSD
Thanks Jim for the examples and reference! Now I need to decide on
the best platform and if I want to replace the batch file or write a
script that I can use in place of the DEL command... If I replaced
the DEL command, I would want to be able to recongize a parameter...
Handling command line parameters is easy in script. For example, in JScript you can do something like this:

function main()
{
if (WScript.Arguments.length != 1)
{
WScript.Echo("<Insert informative error message here>");
return;
}

var Path = WScript.Arguments(0);
var Shell = WScript.CreateObject("Shell.Application");
var Item = Shell.Namespace(0).ParseName(Path);
Item.InvokeVerb("delete");
}
--
Jim Barry, MVP (Windows SDK)
Loading...