Emptying Recycle Bin by VBScript

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Emptying Recycle Bin by VBScript

Post by armsys » Sun Jan 05, 2003 6:04 pm

Would you please show us a script to empty Windows Recycle Bin with a minimal number of VBScript statements? Many thanks in advance.

Ernest

Post by Ernest » Sun Jan 05, 2003 7:09 pm

Hi Armstrong,
I've found this.

Code: Select all

VBSTART
   Sub EmptyRecycler
     set shell = CreateObject("Shell.Application")
     set folder = shell.NameSpace("C:\")
     set item = folder.ParseName("RECYCLED")
     item.InvokeVerb "Empty Recycle Bin"
   End Sub
VBEND
(C) 2001 T.Weltner - Scripting Host - Franzis ISBN 3-7723-7656-8

Please check if "Empty Recycle Bin" matches the option given on your Recycle Bin Context Menu and replace it if necessary.

Rgds,
Ernest

Dalexx

Newbie: The code doesn't run!

Post by Dalexx » Fri Feb 14, 2003 7:00 pm

Hi.
I'm a newbie of macro scheduler and vb script.
I copy the code in this macro


VBSTART
Sub EmptyRecycler
set shell = CreateObject("Shell.Application")
set folder = shell.NameSpace("C:\")
set item = folder.ParseName("RECYCLED")
item.InvokeVerb "Empty Recycle Bin"
End Sub
VBEND

VBRun>EmptyRecycler




but a message error appear like this:

;-2147024894
Line5,column 5

Please help me!
Thank you

Vittorio d'Alessandro

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Fri Feb 14, 2003 7:42 pm

Hi,

This does it in VBScript but unfortunately asks for confirmation:


VBSTART
Sub EmptyRecycler
Dim oShell
Dim oShellFolder
Const ssfDESKTOP = 0
Set oShell = CreateObject("Shell.Application")
Set oShFolder = oShell.Namespace(ssfDESKTOP)
For Each oShFolderItem In oShFolder.Items
If oShFolderItem.Name = "Recycle Bin" Then
oShFolderItem.InvokeVerb("Empty Recycle &Bin")
Exit For
End If
Next
Set oShell = Nothing
Set oShFolder = Nothing
End Sub
VBEND
VBRun>EmptyRecycler


But this is much easier using DOS:


Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
Run>cmd.exe /c rd /s /q c:\recycler
MJT Net Support
[email protected]

Lumumba

Post by Lumumba » Fri Feb 14, 2003 7:49 pm

Is the option you can choose from the Recycle Bin's context menu "Empty Recycle Bin" or is it different!

If you use a non english version you've to replace the entry in the code with the one which is correct for your native OS (as said in the original posting).

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Sun Feb 16, 2003 9:28 am

Dear Support,
Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
Run>cmd.exe /c rd /s /q c:\recycler
With due respect, allow me to raise several questions:
1. c:\Recycler doesn't appear to be a physical directory like other regular directories. As such, would such deletion creates system instability?
2. Even though it's supposed to be self-generated if missing during the Windows bootup, sometimes it fails to do so especially if we have a corrupt Windows registry.
3. Given the fact that c:\recycler is a system directory, can it be deleted by RD?

Look forward to your enlightenment. Thanks.

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Sun Feb 16, 2003 9:51 am

Hi,

The c:\recycler directory does not show up but does exist. It is simply hidden by default. In Windows Explorer under Tools/Folder Options you can elect not to hide system files and folders and you will now see that the recycler folder appears.

You will notice that each user's deleted files exist under different coded directories - one for each user.

I use this technique with no problems and the directory is always recreated. But if you don't want to remove the directory you could just delete it's contents instead using:

c:\
cd recycler
del /q /s *.*
MJT Net Support
[email protected]

Dalexx

Thanks

Post by Dalexx » Sun Feb 16, 2003 12:37 pm

Hi. Thank you .
I've modified your version for the italian language and it works.
This is the code for win2k in It. lang.:

VBSTART
Sub EmptyRecycler
Dim oShell
Dim oShellFolder
Const ssfDESKTOP = 0
Set oShell = CreateObject("Shell.Application")
Set oShFolder = oShell.Namespace(ssfDESKTOP)
For Each oShFolderItem In oShFolder.Items
If oShFolderItem.Name = "Cestino" Then
oShFolderItem.InvokeVerb("S&vuota Cestino")
Exit For
End If
Next
Set oShell = Nothing
Set oShFolder = Nothing
End Sub
VBEND
VBRun>EmptyRecycler


But now I would create a new excel sheet in VB script and I wrote this simple code but it doesn't work, with th same code error.Do you know the reasons?
Thank you again
the code is:


VBSTART
Sub Chiamata
Dim ExcelSheet
Set ExcelSheet = CreateObject("Excel.Sheet")
ExcelSheet.Application.Visible - True
ExcelSheet.ActiveSheetCells(1,1).Value = "This is the column A, row 1"
ExcelSheet.SaveAs "C:\2003\prova4.xls"
ExcelSheet.Application.Quit
Set ExcelSheet = Nothing
End Sub
VBEND
VBRun>Chiamata

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Sun Feb 16, 2003 1:08 pm

Hi,

You need to first create the XL application as the sheet is a property of the workbook property of the application. I have rewritten your code to make it work, as follows:


VBSTART
Sub Chiamata
Dim xlApp
Dim xlBook
Dim xlSheet

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)

xlSheet.Application.Visible = True
xlSheet.Cells(1,1).Value = "This is the column A, row 1"
xlSheet.SaveAs "C:\2003\prova4.xls"
xlApp.Quit

Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
VBEND
VBRun>Chiamata
MJT Net Support
[email protected]

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Sun Feb 16, 2003 1:10 pm

Having done this I could also have replaced:

xlSheet.Application.Visible = True

with

xlApp.Visible = True

Which is a bit neater.
MJT Net Support
[email protected]

dalexx

Thank you

Post by dalexx » Sun Feb 16, 2003 4:48 pm

Thank you so much.
It works great!
Great Macro scheduler.... so i can automate my excel sheets. :D
Thank you again
:wink:


Vittorio d'Alessandro

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts