62

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

Cfinley
  • 1,435
justintime
  • 3,361

14 Answers14

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.

Mark
  • 3,177
27

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,655
10

Can use external utility:

nircmd moverecyclebin *.tmp

8

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
  • 69,547
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

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
5

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
  • 235,242
kizzx2
  • 997
4

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
  • 41
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,313
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
  • 9,176
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.

0

This solution uses VbScript under the hood and does not show a confirmation dialog :

MoveToBin.bat

@Echo Off

Set vTargetDir=D:\Temp
Set vTargetFiles=temp*.txt

Set vbs="%~dp0$$$.vbs" >%vbs% Echo Set shl=CreateObject("Shell.Application"):Set files=shl.NameSpace("%vTargetDir%").Items():Call files.Filter(64,"%vTargetFiles%"):Call shl.Namespace(10).MoveHere(files,1044) cscript %vbs% Del %vbs% 2>&1 >nul

Explanation :

  • Namespace(10) retrieves the user's Recycle bin Folder (see ssfBITBUCKET of the ShellSpecialFolderConstants enumeration)
  • the Folder.MoveHere method get the files to recycle and the Fof_Silent, Fof_NoConfirmation and Fof_NoErrorUi flag
JBress
  • 1
0

Not happy with some ways (MaDdoG and Aikwi are 404 anyways), I made my own tool:

  • pure C, easy to compile (binary available in releases)
  • no output (just 0 exit code on success), unless there's some error
  • no focus stealing, like in MaDdoG Software
  • no wildcards (so far)
  • no confirmations
  • no bloated stuff, so quite fast comparing to other tools (especially MaDdoG with its windowed app approach; my tool is less than 10 bytes short of 1 kB, RecycleIt is whooping 3.2 MB)
  • uses SHFileOperationW and *works in Windows 10.

https://github.com/Krzysiu/cmdwinutils

Krzysiu
  • 285
  • 2
  • 8
-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