Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help integrating 2 ideas

Status
Not open for further replies.

swabs

IS-IT--Management
Jul 28, 2003
155
US
Hello,
I am new to VBScript. Ultimately I am trying to create a script that will check the recyclebin (c:\recycler) and then delete all files in subfolders that are more than 30 days old.

I can successfully delete files in the c:\recycler folder that are more than 30 days old. I can also list the subdirectories in the c:\recycler folder. But I can't figure out how to integrate the 2 into a script that will delete files that are in those subfolders. I have provided the 2 seperate pieces of code below. Any help integrating these would be greatly appreciated.

Path1 = "C:\RECYCLER"

Dim fso
Dim oFolder
Dim oFile
Dim oSubFolder

Set fso = createobject("Scripting.FileSystemObject")

Set oFolder = fso.GetFolder(Path1)

For Each oFile In oFolder.files


If DateDiff("d", oFile.DateCreated,Now) > 10 Then

oFile.Delete True

End If
Next



Set oSubFolder = Nothing
Set oFolder = Nothing
Set fso = Nothing
**************************************************
Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\Recycler")
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Wscript.Echo Subfolder.Path
ShowSubFolders Subfolder
Next
End Sub
 
Something like this ?
Code:
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
DeleteOldFiles fso.GetFolder("C:\RECYCLER"), 30
Set fso = Nothing

Sub DeleteOldFiles(oFolder, intDays)
Dim oSubFolder, oFile
For Each oFile In oFolder.Files
  If DateDiff("d", oFile.DateCreated, Now) > intDays Then
    oFile.Delete True
  End If
Next
For Each oSubFolder In oFolder.SubFolders
  DeleteOldFiles oSubFolder, intDays
Next
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,
thanks very much for your help. I have found a slight problem with my logic. I changed the file attribute from DateCreated to DateLastAccessed because I want to remove items that have been in the bin for more than 30 days. My problem is that I cant seem to find an attribute like "DateDeleted" which would be better than DateLastAccessed. Is there such a function?
The script works great other than that.

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
DeleteOldFiles fso.GetFolder("C:\RECYCLER"), 30
Set fso = Nothing

Sub DeleteOldFiles(oFolder, intDays)
Dim oSubFolder, oFile
For Each oFile In oFolder.Files
If DateDiff("d", oFile.DateLastAccessed, Now) > intDays Then
oFile.Delete True
End If
Next
For Each oSubFolder In oFolder.SubFolders
DeleteOldFiles oSubFolder, intDays
Next
End Sub
 
In case anyone searches this in the future I will post how I resolved this problem. Basically we wanted a solution where user's recycle bins would only have items from the last 30 days.
We used binmanger ( registers binmanager.dll with windows.
We then modified a jscript posted on the web to empty items older than 30 days. (Script at bottom)
So our solution was to run a startup script on workstations that copied the bimanager.dll to the Windows\system32 directory and regsiter the .dll via regsvr32.
We then added the script below to user's the user's logon script via group policy and it will delete items older than 30 days at logon.




var bin = new ActiveXObject("BinManager.RecycleBin");

// Report the contents of the recycle bin.
function Report(){
WScript.Echo("Recycle Bin Holds\n" + bin.NumItems
+ " items\n" + bin.Size + " bytes\n");
}

//Report(30);

// Permanently delete items recycled more than two days ago.
// Note, the argument is full calendar days, so specifying
// zero would delete all items older than today.
bin.DeleteOldItems(30);
//Report(30);

// Permanently delete all recycled items.
//bin.Empty();
//Report();
 
I am new to VB sript, but I realized you can run the same command in VBscript as in Jscript, and it will delete the items older than 30 days.

Dim fso
Set fso = CreateObject("BinManager.RecycleBin")
fso.DeleteOldItems(30)
'Next
'For Each oSubFolder In oFolder.SubFolders
' DeleteOldFiles oSubFolder, intDays
'Next
'End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top