Script to Delete dead messages from C:\inetpub\mailroot\badmail

I have a mail gateway server that is accepting mail for 5 domains and i have about 1000 undeliverble messages  in the  c:\inetpub\mailroot\badmail folder perday what the best way  automatically deleting these messages
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

tigermattConnect With a Mentor Commented:
I've attached below a VBS script which I configure as a Scheduled Task where it is necessary to go through and clear this folder out. The difference with this script is it will only delete mail which is 7 days old (although that value can easily be changed), so it gives you chance to check the Badmail queue before the messages are deleted.

You can simply select the script in the Scheduled Task box and configure it to run once a day, once an hour or at any other interval you choose. The script will then go through and delete all the mail which is more than iDaysOld - change the number assigned to the iDaysOld variable near the top if you want to delete mail which is less old, perhaps only 3 days old.

Dim oFSO
Dim oFolder
Dim oFile
Dim iDaysOld
'Customize values here to fit your needs
      Set oFSO=CreateObject("Scripting.FileSystemObject")
      set oFolder=oFSO.GetFolder("C:\inetpub\mailroot\badmail")
'Walk through each file in this folder collection. 
'If it is older than iDaysOld days, then delete it.
For each oFile in oFolder.Files
If oFile.DateLastModified < (Date() - iDaysOld) Then                               
oFSO.DeleteFile oFolder.Path & "\" & oFile.Name
End If
'Clean up
      Set oFSO = Nothing
      Set oFolder = Nothing
      Set oFileCollection = Nothing
      Set oFile = Nothing

Open in new window

Compaq_EngineerConnect With a Mentor Commented:
Schedule a batchfile to run at a time of low server use using Windows Task Scheduler
The batchfile doesn't need to be anything complicated. I would use as below;-

cd \inetpub\mailroot\badmail
del *.* /Q

Hope that helps.
All Courses

From novice to tech pro — start learning today.