VBscript delete files in recycle bin

Hi

I'm trying to add to a script that is deleting files on drives that are under 3 GB of free space.  Overall the script works fine, except I can't get it to delete the contents of the recycle bin.  Here is what I've tried:

strdrive = "c:"
wscript.echo strComputer & " Checking Recycle Bin"
strdrive = (Left(strdrive,1))
wscript.echo strdrive
'ResFile.writeline vbtab & vbtab & strdrive & " Checking Recycle Bin"
sDirectoryPath = "\\" & strComputer & "\" & strdrive & "$\RECYCLER"
 IF fs.folderexists(sDirectoryPath) then
 ELSE
       sDirectoryPath = "\\" & strComputer & "\" & strdrive & "$\$Recycle.Bin"
 END IF
wscript.echo sdirectorypath
'on error resume next
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder(sDirectorypath)
 
For Each objFile in objFolder.Files
      wscript.echo objfile
      objFile.Delete True
Next

This script runs and does not generate any errors, but it doesn't delete anything.  Any help would be appreciated.  Thank you.

Tony
tonyjoewalkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
Your code looks reasonable.  I think you will need to run it witjh elavated privledges on Vista and Win7, but the rest is okay.

That being said, I think Windows now places the deleted files in subfolders inm the recycle bin, not right at the root.  For example I have the following folders under my recycle bin, with subfolders under some of those, and there are no files in the root of the recycle bin.  You would have to drill down recursively into each subfolder, let me know if you need help with that.

C:\
+-$Recycle.Bin
¦ +-S-1-5-20
¦ +-S-1-5-21-2907544178-1295867664-4172427343-1000
¦ +-S-1-5-21-3146083064-295827163-3641843680-1001

Open in new window

~bp
0
tonyjoewalkerAuthor Commented:
Hi

Thank you for the reply.  Yes, I have noticed that the recycle bin files are in subfolders.  I just want to get rid of everything in the recycle bin of a remote server (I had a server out of disk with 15 GB in the recycle bin today).

Thanks.

Tony
0
Bill PrewCommented:
This should drill down into all files in the recycle bin and delete them.

strDrive = "c:"

Wscript.Echo strComputer & " Checking Recycle Bin"

strDrive = Left(strDrive, 1)
Wscript.Echo strDrive

'on error resume next
Set FSO = CreateObject("Scripting.FileSystemObject")

'ResFile.writeline vbtab & vbtab & strdrive & " Checking Recycle Bin"
sDirectoryPath = "\\" & strComputer & "\" & strDrive & "$\RECYCLER"
If Not FSO.FolderExists(sDirectoryPath) Then
    sDirectoryPath = "\\" & strComputer & "\" & strDrive & "$\$Recycle.Bin"
End If
Wscript.Echo sDirectoryPath

Set objFolder = FSO.GetFolder(sDirectorypath)
 
DeleteFiles objFolder 
 
Sub DeleteFiles(objFolder)
   For Each objFile in objFolder.Files
      Wscript.Echo objFile.Path
      objFile.Delete True
   Next

   For Each objSubFolder In objFolder.Subfolders
      DeleteFiles(objSubFolder)
   Next
End Sub

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tonyjoewalkerAuthor Commented:
That works.  Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.