|
Each contribution is licensed to you under a License Agreement by its owner, not Microsoft. Microsoft does not guarantee the contribution or purport to grant rights to it.
|
Categories |
Delete All the Files in a Directory(Community)
Script Code
VBScript
Option Explicit
'===========================================================================
' Scheduled Task - Visual Basic ActiveX Script
'===========================================================================
' Date : 09/12/2002
' Auth : Israel Farfan
' Desc : VBScript file that deletes old files
'===========================================================================
'-- Empty out "Drop" and "Bad" mail folder from non-sent emails
Call CleanDirectory("C:\Inetpub\mailroot\Drop")
Call CleanDirectory("C:\Inetpub\mailroot\Badmail")
'===========================================================================
' Name : CleanDirectory()
' Desc : Delete Files from a Directory
'===========================================================================
Function CleanDirectory(strPath2Folder)
Dim fso, f, fc, f1, strFiles, intFiles
strFiles = ""
intFiles = 0
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FolderExists(strPath2Folder)) Then
Set f = fso.GetFolder(strPath2Folder)
Set fc = f.Files
'-- Delete each file in Folder
For Each f1 in fc
strFiles = strFiles & f1.Name & vbCrLf
intFiles = intFiles + 1
f1.Delete(True)
Next
Set f1 = Nothing
Set fc = Nothing
Set f = Nothing
'-- Send email with list of files deleted
If Len(strFiles) > 0 Then
strFiles = "Deleted " & intFiles & " File(s) on " & strPath2Folder & vbCrLf & _
vbCrLf & "FILES:" & vbCrLf & "===================" & vbCrLf & strFiles
Call SendAlertEmail("CleanDirectory() Deleted " & intFiles & " File(s)", strFiles)
End if
Else
'-- Path Not Found!
Call SendAlertEmail("CleanDirectory() Failed!", "[ERROR!] File Path not Found : " & _
strPath2Folder)
End If
Set fso = Nothing
End Function
'===========================================================================
' Name : GetMachineName()
' Desc : Determine Computer's Machine Name
'===========================================================================
Function GetMachineName()
On Error Resume Next
'-- Try Network Object
Dim WshNetwork
Set WshNetwork = CreateObject("WScript.Network")
GetMachineName = WshNetwork.ComputerName
Set WshNetwork = Nothing
If IsNull(GetMachineName) Or GetMachineName = "" Then
'-- Try Shell Object
Dim wshShell
Set wshShell = CreateObject("WSCript.Shell")
GetMachineName = wshShell.RegRead _
("HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName")
Set wshShell = Nothing
If IsNull(GetMachineName) Or GetMachineName = "" Then
'-- Try Windows Mgmt Implementation
Dim objWMIService, OSItems, objItem
Set objWMIService = GetObject _
("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set OSItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem", , 48)
For Each objItem in OSItems
GetMachineName = objItem.CSName
Next
End If
End If
Err.Clear
If IsNull(GetMachineName) Or GetMachineName = "" Then GetMachineName = "LOCALHOST"
End Function
'===========================================================================
' Name : SendAlertEmail(Subject Line, Email Body)
' Desc : Send Error/Alert Email
'===========================================================================
Function SendAlertEmail(strSubject, strErrorText)
'-- Get Machine Name
Dim oEmail, strMachineName
strMachineName = GetMachineName()
'-- Send Email
Set oEmail = CreateObject("CDO.Message")
oEmail.From = strMachineName
oEmail.To = "myemail@mydomain.com"
oEmail.Subject = strMachineName & " : " & strSubject & " @ " & Now
oEmail.TextBody = strErrorText
oEmail.Send()
Set oEmail = Nothing
End Function
Platforms
For online peer support, join
The Official Scripting Guys Forum!
To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script.
Disclaimer
The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
Be the first to create a discussion.
|