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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script for file cleanup 2

Status
Not open for further replies.

PeteAmsterdam

Programmer
Jun 3, 2005
76
US
Hello I need to create a VB Script/or batch pgm to delete all files prior to 10 days from a given directory in windows.

Perhaps some one has some codes already that does this or similar?

Thank you,
Peter
 
As a matter of fact I do...

First directory just deletes files, second one deletes sub folders.

Code:
'==========================================================================
'
' VBScript Source File -- 
'
' NAME: CleanBadMail.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL   : [URL unfurl="true"]http://www.thespidersparlor.com[/URL]	
' Copywrite (c) 2003 All rights reserved
' DATE  : 09/10/2003
'
' COMMENT: 
'
' This script will list all filtered and quarantined SPAM mail, check that 
' the files are more than 30 days old and then delete them.
' This file is to be scheduled to run each day.
'=====================================

Path1 = "E:\Program Files\Exchsrvr\Mailroot\vsi 1\BadMail"
Path2 = "E:\Program Files\Trend\SMCF\Quarantine"


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) > 30 Then
    	oFile.Delete True
    End If
  Next


Set oFolder = fso.GetFolder(Path2)
Set colSubfolders = oFolder.Subfolders

For Each oSubfolder in colSubfolders
   	If DateDiff("d", oSubFolder.DateCreated,Now) > 30 Then
		fso.DeleteFolder(oSubFolder)
	End If
Next

Set oSubFolder = Nothing
Set oFolder = Nothing
Set fso = Nothing

I hope you find this post helpful.

Regards,

Mark
 
Mark - thanks I will try this. How would you go about making this an executable program ?

THanks,
Peter
 
Juist save the text to a text file and replace the TXT extension with VBS.

I hope you find this post helpful.

Regards,

Mark
 
Mark, Thanks, You the MAN! I may want to add some functionality to this. To deploy to users and have it prompt for the folder for example.
Is that an easy add?
Thank you,
Peter
 
Or if there are other ways to make this a more interesting script, for learning purposes. Like a parameter for number of days to delete and so on?
 
have it prompt for the folder
Function PickFolder(strStartDir)
Dim SA, f
Set SA = CreateObject("Shell.Application")
Set f = SA.BrowseForFolder(0, "Choose a folder", 0, strStartDir)
If (Not f Is Nothing) Then
PickFolder = f.Items.Item.Path
End If
Set f = Nothing
Set SA = Nothing
End Function

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PH, I put this after the DIM statements, ran it but did not get the prompt?
 
A starting point:
MsgBox "Choosen dir: " & PickFolder("C:\")

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Yes thank you PH that works, of course this is a dangerous idea as one could delete the wrong thing.
 
I suupose "Last Accessed Date ' might be more interesting. How is this accessed into the basic program?
 
Replace DateLastModified by DateLastAccessed.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Pete,

You asked about the number of days, In the script it uses 30 and you could change that for however old you want the file to be before it is deleted. If you want the script to prompt for it then replace the 30 with "daysold" (without the quotes) and add this line near the top of the script:

Code:
daysold = InputBox("How many days old should the file be?","How Old?")

I don't however advise doing this. The idea of this script is to be able to automate the process on a server. Set your path and number of days and then use the Scheduler to have it fire off each day.

I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top