Search code examples
batch-filevbscriptrecycle

Recycle the CONTENTS of a folder, but not the folder itself with either a Batch or VBS file


The title pretty much says it all. My goal is to create some kind of file that can be executed by a double click (batch, for example) where I specify (in the file) the path to the folder that, does not need to be sent to the recycle bin, but have its CONTENTS sent to the recycle bin. See, I'm doing this for a Rainmeter skin. I have a lot of temporary files and folder created by Adobe, and I can control where they're placed at. However, there can be quite a few of them that stack up inside the folder I told them to be in, so I need find some way to create a file that will recycle all of the files and folders within the specified folder (but not recycling or deleting the specified folder). This way, the specified folder will still exist so that I don't have to make a new folder and force Premiere Pro to relocate to a new folder (in the instance that I delete the folder containing the files needing to be deleted, which I've already figured out how to do) each time that I use this script.

I've tried a utility called "Recycle.exe" but I can only figure out how to get it to recycle the specified folder rather than recycle its contents. For example, when I try the following in a batch file (when using the following utility: http://www.maddogsw.com/cmdutils/):

@echo off
Recycle D:\TestFolder
pause

This will send the folder named "TestFolder" to the Recycle Bin, as well as its contents (the same way they'd be sent to the recycle bin if you just did it normally). My goal is to keep the specified folder intact, still existing, but having ALL of its contents, not deleted, but sent to the Recycle Bin. I've tried numerous things but have been unsuccessful. I don't really care what it takes to achieve this, but the final product has to result in a file that can be double clicked and send the contents of the specified folder to the Recycle Bin. VBS and Batch files were just examples, but I'm sure that there might be other ways to do it. I really need your help because I'm extremely close on this one.

(Also, please don't ask why I'm sending it to the Recycle Bin instead of deleting it, I'm doing it simply for the safety in case I determine that I need to restore the media cache/preview files. Keep in mind that sending the contents of the folder to the Recycle Bin instead of deleting them is an utter crucial function that I'm looking to accomplish with this, however.)


Solution

  • Since you can specify files and have a predetermined folder where you're keeping them, you can use a for loop to iterate through the list of files in the directory.

    pushd D:\TestFolder
    for /F %%A in ('dir /b') do Recycle %%A
    popd
    

    You may want to say echo Recycle %%A first to make sure it's going to grab everything you want.

    The documentation for recycle.exe claims you can use wildcards, so in theory you could simply say recycle D:\TestFolder\*.* instead of using a for loop.