57

Is there a command line program that can send files to the recycle bin? This is on XP and Vista.

Cfinley
  • 1,435
  • 3
  • 14
  • 20
justintime
  • 3,201
  • 5
  • 29
  • 33
  • 2
    Other than 'del'? – pelms Aug 18 '09 at 16:50
  • 2
    Which OS are you using? – ChrisF Aug 18 '09 at 16:51
  • exited to add XP and Vista – justintime Aug 18 '09 at 16:59
  • 18
    @pelms `del` permanently deletes it, not moves it to the recycle bin. – MiffTheFox Aug 18 '09 at 17:02
  • Why are you looking to move them to the recycle bin? If we can understand your thinking, there may be a better way. – EvilChookie Aug 18 '09 at 17:40
  • @EvilChookie, um, what? Are you asking why the Recycle Bin exists at all? Because sometimes you want to delete a file with the option of possibly restoring it. There’s no difference whether you do this from Explorer or the command-line. – Synetech Oct 01 '12 at 02:39
  • @Synetech: No, that's not what I was asking. I was confused as to why you would want to move a large batch files (since he's doing this via cmd) to the recycle bin - why not just move them to a temporary location? The recycle bin is not for file storage or version control. I've had a lot of bad experiences with users who felt this way (Oh, I can just restore it later) only to forget what's in there, and wipe it all away. That's like putting a fax into the rubbish bin and grabbing it out if you need it. Why not put it somewhere safe until you know you can throw it away? – EvilChookie Oct 02 '12 at 21:14
  • 2
    Then you must think there is no reason to have the Bin at all. By your logic, why recycle files from Explorer instead of just deleting them permanently? You may not have seen a use, but they do exist. Just recently, I wanted to delete a pile of files from numerous folders, but wanted to double-check them before wiping them out. The only/easiest way to do it was to run a `for` loop from the command-prompt to send them to the bin, then I could see them all consolidated in one place to make sure that only the files I wanted to delete were in there and restore any incorrect files. – Synetech Oct 03 '12 at 00:32
  • Be aware that pretty much all of the solutions (including my own) may not work with files that have Unicode (or any non-Latin) characters in the filenames. This may be a limitation of the program(s), but it may also just be a consequence of the command-prompt. Changing the code-page with the `chcp` command might help, but it may still not work for all files. – Synetech May 08 '20 at 21:18

12 Answers12

40

CmdUtils has a utility called Recycle that does exactly that. [direct download]

More info:

To use the recycle command download the CmdUtils zip file and unzip the exe to your Windows folder. Adding them to the Windows folder would allow you to access the command globally without you having to specify the entire path to the executable. You can then start using the recycle command by typing in;

recycle filename.txt

You can also specify wildcards with the commands so typing in recycle *.txt will recycle any text files in the current directory. There is also a option to suppress the delete confirmation dialog by using the force flag with the command.

To delete a file without having to confirm is use the command

recycle –f filename.txt

The –f flag will tell the command to force a recycle without showing you the confirmation dialog.

laggingreflex
  • 5,035
  • 17
  • 65
  • 96
Mark
  • 3,149
  • 9
  • 36
  • 39
25

If you have powershell installed:

$sh = new-object -comobject "Shell.Application"
$ns = $sh.Namespace(0).ParseName("PATH\TO\FILE\TO\DELETE")
$ns.InvokeVerb("delete")
EBGreen
  • 9,127
  • 1
  • 36
  • 40
8

Can use external utility:

nircmd moverecyclebin *.tmp

Yura Shinkarev
  • 191
  • 1
  • 8
7

There is no built-in way to do this, but there are third-party tools that can. I checked my program-dump folder and found a few options. They all work the same (e.g., recycle filename.ext), but they vary in performance, so it depends on what your needs are (e.g., are you recycling a lot of files?)

  • MaDdoG Software’s Recycle is fast and has no output, but can throw a mysterious not-found error
  • EasyTools’ DeleteXP is slow because it displays the progress to the console, but if you redirect it to nul, then it is the fastest and reliable
  • Chris Yuen’s cmd-recycle is slowest, even when redirecting the (poorly formatted) output to nul
Synetech
  • 68,243
  • 36
  • 223
  • 356
6

Just improving upon EBGreen code, here is a convenient 1 line code that can be easily put into any batch file to get the job done. (assuming the fully qualified filepath is in %1 batch file parameter)

echo (new-object -comobject Shell.Application).Namespace(0).ParseName("%~1").InvokeVerb("delete") | powershell -command - 

Note the trailing - it make the powershell command to accept command text from stdin

clueless
  • 61
  • 1
  • 1
  • 1
    This allows both the fully qualified path name & filename to be used as the param: `echo (new-object -comobject Shell.Application).Namespace(0).ParseName("%~f1").InvokeVerb("delete") | powershell -command -` – Jimadine Jul 01 '20 at 18:50
  • spawning a new shell isn't an improvement. It'll be much slower. Why don't just use `Invoke-Expression` to evaluate the command? – phuclv Jun 22 '21 at 08:49
5

Without using third party tools I don't believe there is a "command line way of sending files to the recycle bin". You can get the full path to recycle bin on a Windows 7-10 system like this:

::get current user sid
for /f "tokens=2" %%i in ('whoami /user /NH') do set UID=%%i
:: create full path to current user recycle bin in a variable
set recyclebin=%systemdrive%\$Recycle.Bin\%UID%

echo %recyclebin%

The problem is that if you just move a file in there it doesn't appear in the recycle bin. You will only be able to see it in a command prompt. The recycle bin is a special folder. The windows API method of moving items to the recycle bin renames the file and stores information about it in a proprietary info file or files depending on the version of the OS. The third party tools suggested in the answers above invoke these API methods that handle all of that for you.

Some more info here: https://dereknewton.com/2010/06/recycle-bin-forensics-in-windows-7-and-vista/

TroyK
  • 96
  • 1
  • 3
  • Welcome to Super User. Your answer seems to explain how to build the path to the currently logged on user's Recycle Bin, but it does not appear to explain how to use this information to **send files to the recycle bin** as requested by the OP. Please edit your post to include this last part. Thanks for contributing. – I say Reinstate Monica Jan 18 '17 at 04:35
  • This has almost all the information you'd need to write your own, short of actually presenting you with one, except mentioning the INFO2 and $I data which you'd need to manipulate. – can-ned_food May 21 '18 at 16:55
  • Let me know when the answer includes a cmd.exe or wsh.exe script which does so, and then I'll upvote! :-) Well, i guess wsh isn't technically applicable to the question, but the question is rather vague. – can-ned_food May 21 '18 at 17:36
  • Note that you should run this from a bat file, and should not directly from cmd.exe. `%%`, and `%` have different meanings for commands run directly from cmd.exe, and for commands run from bat file. see https://stackoverflow.com/a/14510049/981766 – Sahil Singh Jul 30 '21 at 08:28
4

I've had this question for a long time -- I finally took the matters into my own hand and I rolled my own utility cmd-recycle

I took a look at Recycle.exe in CmdUtils. The thing about it is that it pops out the traditional "Are you sure" dialog when you recycle (which can be removed by adding the -f argument). My program just does it (since you can always undo) which I think is more suitable for scripting purposes.

slhck
  • 223,558
  • 70
  • 607
  • 592
kizzx2
  • 967
  • 10
  • 9
  • I just ran it on Windows Server 2008R2 and it doesn't work. – dthree Mar 30 '16 at 20:49
  • Doesn't work on Windows 7 either. I just get an error "The application could not be started..." and if i click for more info, I'm taken to [microsoft.com :".NET Framework initialization errors: Managing the user experience"](https://docs.microsoft.com/dotnet/framework/deployment/initialization-errors-managing-the-user-experience). – ashleedawg Jun 15 '19 at 11:34
3

I tried various programs for moving a file(s) to the recycle bin, but was unsatisfied with them for various reasons.

The main problem most have is the lack of decent status or error messages. Some just fail silently, so you think the program recycled something but in fact didn't do anything at all!

To remedy this, I've written a command line utility called bin-it that moves the specified file(s) to the Windows recycle bin. It supports wildcards and provides full status and error reporting. If something goes wrong, you'll know about it!

It's completely free and can be downloaded from here as binit.zip:
http://www.akiwi.co.uk/utilities.html

akiwi
  • 31
  • 2
  • I recommend you change the name. “Bin It” makes me think of something like an ASCII->binary conversion. – can-ned_food May 21 '18 at 17:35
  • Just downloaded binit - thanks! One suggestion (though I note the binary has not been updated since 2015): obey the standard "/?" help listing for DOS/Windows command line programs. Was alarmed when I typed "binit /?" and got the reply, "Recycling c:\?, [OK]" - although nothing happened. – cniggeler Jan 19 '20 at 03:03
  • +1 Really like the fact that it can process a list of files to delete from the command line. – Anaksunaman Jun 18 '21 at 07:48
  • Thanks mate... really good alternative. I was using a recycle.exe, small one, I don't remember where I got it from, but it would hang sometimes in an unkillable state... really frustrating. – JasonXA Mar 07 '23 at 00:34
1

Without external programs - deleteJS.bat. It uses Shell.Application invoke verb method. usage is simple:

call deleteJS.bat c:\someFile.txt
call deleteJS.bat d:\someFolder
npocmaka
  • 1,259
  • 12
  • 12
  • I can certainly appreciate a poly-glot program, but this asks for confirmation to recycle the file, which makes it unscriptable. And turning off Recycle Bin confirmation system-wide isn't practical. :-\ – Synetech May 08 '20 at 21:33
  • @Synetech - what do you mean? I've just tested this and it didn't asked me confirmation. What OS are you using? What kind of file you've tried to delete? It was ok even with read-only file. – npocmaka May 09 '20 at 04:00
  • You must have confirmation turned off system-wide (it won't ask when you delete files in Explorer). – Synetech May 10 '20 at 04:05
1

You can use this VBScript function:

Function RecycleFile(strFilePath)
  Dim objShell,objFolder,strFolder,strFile,arrTabs,strTab,intCount
  intCount = 0
  Set objShell = CreateObject("Shell.Application")
  arrTabs = Split(strFilePath,"\")
  strFile = arrTabs(UBound(arrTabs))
  For Each strTab in arrTabs
    If intCount = UBound(arrTabs) Then Exit For
    strFolder = strFolder & strTab & "\"
    intCount = intCount + 1
  Next
  Set objFolder = objShell.Namespace(strFolder)
  objFolder.ParseName(strFile).InvokeVerb("delete")
End Function
Wasif
  • 7,984
  • 2
  • 19
  • 32
  • Using windows explorer the delete confirmation is the default setup and I would not recommend disabling it. `InvokeVerb("delete")` will trigger a delete confirmation window for each `strFilePath` file. `strFilePath` has to be a full path as well. Is there a way to supress the delete confirmation prompt when using this VBScript? – Scott R May 16 '21 at 05:50
0

You can try RecycleIt. It will send files to the Windows Recycle Bin via command line.

EXAMPLE USAGE:

recycleIt.exe C:\temp\example.txt /quit

NOTE: You need to add the "/quit" or it will pop a window that stays open. This could be problematic for headless console sessions.

-3

We can use the “recycle” command to delete any file straight to the Recycle Bin.

Recycle File.txt

To delete multiple files, use the following command:

Recycle File1.txt File2.txt