none
Empty Recycle Bin From Batch File

    Question

  • Hello,

    I need help with emptying the recycle bin from the command line.  I think it involves using the rundll32 command to call the function SHEmptyRecycleBin; Also, Can someone help with using Cleanmgr from the command line, I want to run cleanmgr from a batch file and when it runs I want it to automatically clear EVERYTHING, with no user input and I DONT want to use the sageset or sagerun switches. Your help is much appreciated!


    Brandon Jimenez
    Tuesday, September 27, 2011 9:35 AM

Answers

All replies

  • I dont know how you can use cleanmgr without sageset or sagerun but you could also do this to empty the Recycle Bin:

    ATTRIB %systemdrive%\RECYCLER\* -R -S -H /S /D
    RD %systemdrive%\RECYCLER /S /Q

    Put the above in a batch file, and that's it..

    Sometimes RD won't do it's job, so to be safer side follow these :

    ATTRIB %systemdrive%\RECYCLER\* -R -S -H /S /D
    DEL %systemdrive%\RECYCLER\* /F /S /Q
    RD %systemdrive%\RECYCLER /S /Q
    BR
    Mads

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Tuesday, September 27, 2011 9:57 AM
  • Hi,

    There's also EmptyRecycleBin.exe.

    Bill

    Tuesday, September 27, 2011 2:17 PM
    Moderator
  • Mads Hjort Larsen,

    Im running windows 7 and that path does not esist :( and BR and Mads are not native commands on my computer...


    Brandon Jimenez
    Tuesday, September 27, 2011 3:46 PM
  • AbqBill,

    Thanks for the advice, however, I am more interested in emptying the recycle bin using native commands.  Im running windows 7.


    Brandon Jimenez
    Tuesday, September 27, 2011 3:48 PM
  • Hi,

    There is no native command to empty the recycle bin. Hence the reason Marty List (optimumx.com) wrote the executable that calls the relevant API.

    Bill

    Tuesday, September 27, 2011 4:19 PM
    Moderator
  • AbqBill,

    I realize that, but I want to call the API myself using rundll32, I did some research, and the function is SHEmptyRecycleBin and the syntax is:

    HRESULT SHEmptyRecycleBin(
      __in_opt  HWND hwnd,
      __in_opt  LPCTSTR pszRootPath,
      DWORD dwFlags
    );

    Im just not sure how to incorporate the above code in a batch file, click on the link...


    Brandon Jimenez
    Tuesday, September 27, 2011 4:49 PM
  • AbqBill,

    I realize that, but I want to call the API myself using rundll32, I did some research, and the function is SHEmptyRecycleBin and the syntax is:

    HRESULT SHEmptyRecycleBin(
      __in_opt  HWND hwnd,
      __in_opt  LPCTSTR pszRootPath,
      DWORD dwFlags
    );

    Im just not sure how to incorporate the above code in a batch file...


    Brandon Jimenez


    YOu can't. It is an API call and can  only be called from a compiled level language like C or C#.  The template you are posting is the C++ template forem the header file.

    You can probaly call it from a rundll if the shell exposes the oridinal by number or name and if it is documented.

     

     


    jv
    Tuesday, September 27, 2011 5:01 PM
  • Hi,

    If an API function doesn't follow a specific function signature, you cannot reliably call it from rundll32. Trying to do this using rundll32 will result in a corrupted stack.

    Bill

    Tuesday, September 27, 2011 5:06 PM
    Moderator
  • Her is the equivalent as a shell command in VBScript:

    Const CLSID = "::{645FF040-5081-101B-9F08-00AA002F954E}"
    
    Set oShell = CreateObject("Shell.Application")
     
    Set oRecycleBin = oShell.Namespace(0).ParseName(CLSID) 
    oRecycleBin.InvokeVerb "Empty Recycle &Bin"
    
    
    
    
    

     


    jv
    Tuesday, September 27, 2011 5:06 PM
  • Hi,

    Yes, trying to call it from Shell.Application will work if that verb exists in exactly that form in the shell namespace. Not good for localized versions, though.

    Bill

    Tuesday, September 27, 2011 5:07 PM
    Moderator
  • jrv,

    Well I copied your code and saved it as a .vbs file, but when I ran the script nothing happened :(


    Brandon Jimenez
    Tuesday, September 27, 2011 6:24 PM
  • Hi,

    I checked and the shell verb exists (at least on my Windows 7), but trying to invoke the verb from script doesn't do anything for me either.

    I suggest EmptyRecycleBin.exe.

    Bill

    Tuesday, September 27, 2011 6:46 PM
    Moderator
  • Hi,

    I checked and the shell verb exists (at least on my Windows 7), but trying to invoke the verb from script doesn't do anything for me either.

    I suggest EmptyRecycleBin.exe.

    Bill


    It takes an argument I believe.  The VBscript code makes the same call into the shell. It uses the verb and may cause a prompt.  The shell will takes an argument that suppresses teh promp. ,0 or ,1 I believe.

     

     


    jv
    Tuesday, September 27, 2011 6:56 PM
  • jrv,

    Well I copied your code and saved it as a .vbs file, but when I ran the script nothing happened :(


    Brandon Jimenez


    What OS?

     


    jv
    Tuesday, September 27, 2011 6:58 PM
  • Everybody,

    I found something interesting... while I was fooling around with the command console I came accross a hidden system folder called $Recycle.Bin located in C: drive (the path is C:\$Recycle.Bin).  Maybe this is an entry point to the recycle bin??  Try it for yourself, change the directory to root (CD\), then run attrib $Recycle.Bin and you'll see it.  Any ideas on what I could do from here??  I tried to remove the Hidden attribute but Access IS DENIED. A penny for your thoughts...


    Brandon Jimenez
    Tuesday, September 27, 2011 7:03 PM
  • It takes an argument I believe.  The VBscript code makes the same call into the shell. It uses the verb and may cause a prompt.  The shell will takes an argument that suppresses teh promp. ,0 or ,1 I believe.

    The InvokeVerb method doesn't have any arguments other than the verb to be invoked, and it returns nothing.

    Bill

    Tuesday, September 27, 2011 7:11 PM
    Moderator
  • You are on Windows 7. None of the above methods will work.

     


    jv
    Tuesday, September 27, 2011 7:11 PM
  • I tried to remove the Hidden attribute but Access IS DENIED. A penny for your thoughts...

    That is by design. The only valid way to empty the recycle bin is via the API. This is what EmptyRecycleBin.exe does.

    Bill

    Tuesday, September 27, 2011 7:14 PM
    Moderator
  • It takes an argument I believe.  The VBscript code makes the same call into the shell. It uses the verb and may cause a prompt.  The shell will takes an argument that suppresses teh promp. ,0 or ,1 I believe.

    The InvokeVerb method doesn't have any arguments other than the verb to be invoked, and it returns nothing.

    Bill


    Not what I am saying.

    Rundll shell.dll EmptyRecleBin,1

    That is what takes the optional argument not InvokeVerb.

     


    jv
    Tuesday, September 27, 2011 7:28 PM
  • Rundll shell.dll EmptyRecleBin,1

    Hi,

    You can't use rundll32 to call the SHEmptyRecycleBin API function because the function signature does not match. Rundll32 can only execute certain API functions. Trying to use it out of scope results in a corrupted stack.

    HTH,

    Bill

    Tuesday, September 27, 2011 7:31 PM
    Moderator
  • Her Bill is a famous one -

    printto HTML
    rundll32.exe C:\WINDOWS\SYSTEM\MSHTML.DLL,PrintHTML "%1" "%2" "%3" "%4"

     


    jv
    Tuesday, September 27, 2011 8:07 PM
  • Here are some but not all of the shell calls.

    http://vb.mvps.org/tips/shellcpl.txt

    These are old.  I am pretty sure the list predates Vista and it wan't even complete then.

     


    jv
    Tuesday, September 27, 2011 8:10 PM
  • rundll32.exe C:\WINDOWS\SYSTEM\MSHTML.DLL,PrintHTML "%1" "%2" "%3" "%4"

    Of course, this works since the PrintHTML function in MSHTML.dll is designed to be called from rundll32.exe. Note that this has nothing to do with COM (ActiveX).

    HTH,

    Bill

    Tuesday, September 27, 2011 8:18 PM
    Moderator
  • Here are some but not all of the shell calls.

    http://vb.mvps.org/tips/shellcpl.txt

    These are old.  I am pretty sure the list predates Vista and it wan't even complete then.

    Aside from being an old list, there is a good likelihood that most of these won't work on newer versions of Windows. Read What can go wrong when you mismatch the calling convention? for why this is the case.

    HTH,

    Bill

    Tuesday, September 27, 2011 9:03 PM
    Moderator
  • Here are some but not all of the shell calls.

    http://vb.mvps.org/tips/shellcpl.txt

    These are old.  I am pretty sure the list predates Vista and it wan't even complete then.

    Aside from being an old list, there is a good likelihood that most of these won't work on newer versions of Windows. Read What can go wrong when you mismatch the calling convention? for why this is the case.

    HTH,

    Bill


    Most of them work just fine on all versions of WIndows.  SOme have been altered in Win7.

    Here is the invoke for Win7 but I haven't yet discovered the corerct verb varialtion.  This does access the new Recycle Bim manager.

    Const CLSID = "::{645FF040-5081-101B-9F08-00AA002F954E}"
    
    Set oShell = CreateObject("Shell.Application")
     
    Set oRecycleBin = oShell.Namespace(0).ParseName(CLSID) 
    oRecycleBin.InvokeVerb -10564
    
    

     


    jv
    Tuesday, September 27, 2011 9:34 PM
  • By the way.  The pervious code  I posted wotk just fine on all versions of WIndowsbefore Vista.  Frr Vista and later the verb has been changed becuse it is the newer Recycle bin manager.

     


    jv
    Tuesday, September 27, 2011 9:36 PM
  • Of course, if it works, it greatly increases the complexity of calling scripts as it seems like a script would need to have some OS detection logic to determine which branch of code to run. (This may all be beside the point because the subject line of this question asks for a shell script ["batch file"])

    Bill

    Tuesday, September 27, 2011 9:47 PM
    Moderator
  • This also work to empty recycle bins before Vista:

    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set shell = CreateObject("Shell.Application")
    Set bin = shell.Namespace(10)
     
    For Each item In bin.Items()
        If item.Type = "File Folder" Then
             WScript.Echo "Folder:" & item.Path
            'fso.DeleteFolder(item.Path)
        Else
             WScript.Echo "File:" & item.Path
            'fso.DeleteFile(item.Path)
        End If
    Next
    
    
    

     


    jv
    Tuesday, September 27, 2011 9:48 PM
  • Of course, if it works, it greatly increases the complexity of calling scripts as it seems like a script would need to have some OS detection logic to determine which branch of code to run. (This may all be beside the point because the subject line of this question asks for a shell script ["batch file"])

    Bill


    So put the script in a VBS file and call that from the batch file.  What is the putpose of emptying the users trash anyway? 

    We used to turn  off the trash bin on Terminal Servers to save disk and fragmentation.  We don't do that anymore as the disks are all very large. We also move users forlders. 

    WS2008 puts the recycle bin into My Documents so all is well.

    The scripts above work perfectly in XP and WS2003 which is where they are likely to be needed.

    I will leave it to someone else to track down the ordinal for the recyling manager as I don't see where is is that useful.

     

     


    jv
    • Edited by jrv Tuesday, September 27, 2011 10:05 PM
    Tuesday, September 27, 2011 10:04 PM
  • I know this is an outdated thread now.  But since I was looking for an answer to cleaning the recycle bin myself and managed to find a way to do so I thought I would post it here.  copy this text and save as .bat to clean it from a batch file.  This clears out the contents of the recycle bin for all users.  If you are running Windows xp change $Recycle.bin to Recycler

    @echo off

    cd c:

    cd..

    cd..

    dir /a

    attrib -h -s c:\$Recycle.bin /s /d

    del c:\$Recycle.bin\*.* /f /s /q

    pause

    Tuesday, April 30, 2013 6:11 PM
  • @echo off
    cd c:
    cd..
    cd..
    dir /a
    attrib -h -s c:\$Recycle.bin /s /d
    del c:\$Recycle.bin\*.* /f /s /q
    pause

    Hi,

    Your code contains four superfluous commands (all the cd commands and the dir command), and would also potentially work for only the C: partition. Also, the pause command is not needed (and actually hampers automation, because it stops the script).

    In general it is not advisable to change the attributes (attrib) for protected system directories, so I certainly do not recommend that anyone use this code.

    Also, this thread is already marked as answered. If you still have questions, please start a new thread.

    Bill

    Tuesday, April 30, 2013 6:40 PM
    Moderator
  • I absolutely agree with Bill.  That is a VERY dangerous move for many reasons.

    If you want a simple commandline methiod that works from PowerSHell this is it:

    dir \recycler -force -Recurse |?{-not $_.PsIsContainer}|Remove-Item -force

    This does not alter the folder settings and removes all files quietly and very very fast.

    Under Vista and later you may have to stop the service before delting the files.

    The easiest way is to use the Shell extensions to do this.

    In a domain ,  when we redirect teh users Documents folder the users recycler is also redirected.  YOU can then let teh user maneg their folders and set a quota on teh redirected folder and let that hassle the user about space usage.  Microsoft has spent nearly three decades studying how to empower users and reduce management demands.  We should not keep trying to re-invent the wheel.


    ¯\_(ツ)_/¯

    Tuesday, April 30, 2013 6:56 PM