Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a way to move files from the Windows Recycle Bin to another folder using a batch script?

Background: I have a folder named C:\Temp on my computer. I clear this folder every week using a batch script. I would like to use this C:\Temp folder as the default place for all of my deleted files. Unfortunately, per this post, I learned that you cannot change the default place where Windows deletes files and folders to. I was wondering, however, if it is possible to use a batch script to move files out of the Recycle Bin and into my C:\Temp folder.

Note: If there is another method, perhaps one that doesn't use a batch file, I am open to that as well.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

This could be done using a PowerShell script as follows:

$shell = New-Object -ComObject Shell.Application  
$recycleBin = $shell.Namespace(0xA) #Recycle Bin  
$recycleBin.Items() | %{Move-Item $_.Path ("C:\Temp\{0}" -f $_.Name)}   
  • The directory structure of deleted folders is maintained upon moving to the destination folder.
  • The destination folder (C:\Temp in this case) must exist before the script is run or the files won't be moved.
  • A file won't be moved if a file of the same name already exists in the destination location. This could be handled by catching the error and appending the Name with something to make it unique.
  • The action of the script is specific to the current user's Recycle Bin.

Script is a modified version of this one from the Microsoft Script Center Repository.

share|improve this answer

You can access the Recycle Bin in the path C:\$Recycle.bin so you can use the xcopy command in your script. Take into account that you can have a folder like that in more than one drive.

I would xcopy all files and folders from the Recycle Bin and in the next line I would delete all its contents.

The script will be like this:

xcopy C:\$Recycle.bin C:\Temp /E /R /Y
rd /s /q C:\$Recycle.bin

There is no way the Recycle Bin can be issued an "empty" command. By deleting this folder you are in fact emptying it. Windows recreates this folder as needed.

You can read the command line reference in Microsoft's Technet to learn about all the available parameters for xcopy and rd.

To address @JdeBP remarks: If you have more than one user account in your computer, you can use C:\$Recycle.bin\USER-SID instead. To find out the different SID and the one that corresponds to your user account, use this registry entry.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

The ProfileImagePath key contains the user name.

share|improve this answer
1  
An individual account's recycle bin is actually a subdirectory of that directory; and one will hit problems with the above simplistic approach if one tries to mess with other accounts' recycle bins without appropriate access rights. –  JdeBP Feb 12 at 12:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.