Batch script move files from Windows Recycle Bin
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.
Related videos on Youtube
jsve
View my resume, go to my personal website, or explore my LinkedIn Profile. I'm a Software Engineer at Beeper and an Adjunct Professor at Colorado School of Mines. At Beeper, I work as a bridge developer, helping to bring more people into the Beeper network via bridges. I primarily program in python, go, js, and sql for work. I write most of my side projects in python or go. Other languages which I am reasonably proficient in include c#, c/c++, rust, html, and css. I am interested in Linux, software architecture, algorithms, distributed systems, cloud computing, and Free and Open Source software, and CS education. In addition to programming, I play soccer, volleyball, and racquetball and I am a 3rd Degree Black Belt and Certified Instructor in ATA Taekwondo. License: All of my code contributions to StackOverflow are licensed under the Unlicense. Attribution is not required, but appreciated.
Updated on September 18, 2022Comments
-
jsve almost 2 years
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 thisC:\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 myC:\Temp
folder.Note: If there is another method, perhaps one that doesn't use a batch file, I am open to that as well.
-
JdeBP over 10 yearsAn 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.