I want to monitor this folder for file creation, and on file creation send myself an e-mail.
However, I only want to have it email me and tell me about the file if it's of a specific file extension. i.e only email me if the file extension of the newly created file is .doc.
I have successfully created code that gives a little pop-up when a file is created in a folder, then moved on to make it email me. However, I cannot seem to go on to the next steps of making it watch for file creation in all subfolders. I am certain I can add the code myself for checking the file extension...
Here is my code so far. Can anyone spare some time to help me please?
sComp = "."
sFolder="c:\\\\test"
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComp & "\root\cimv2")
Dim Queue
Queue = sFolder & ";"
While Queue <> ""
Dim Path
Path = Split(Queue,";")(0)
Queue = Right(Queue,Len(Queue) - Len(Path)-1)
Dim oFSO, oFolder, oFiles, oFile, OSubFolders
set oFSO = CreateObject("Scripting.FileSystemObject")
set oFolder = oFSO.GetFolder(Path)
set oFiles = oFolder.Files
set oSubFolders = oFolder.SubFolders
For Each oSubFolder in OSubFolders
Set oEvents = oWMI.ExecNotificationQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE " & "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " & "TargetInstance.GroupComponent= " & "'Win32_Directory.Name=""" & oSubFolder & """'")
Do
Set oEvent = oEvents.NextEvent
sNewFileName=oEvent.TargetInstance.PartComponent
Wscript.Echo sNewFileName
Set objMessage = CreateObject("CDO.Message")
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "removed"
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = removed
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update
objMessage.Subject = "Unwanted file created"
objMessage.From = "removed@removed.removed.removed.removed"
objMessage.To = "removed@removed.com"
objMessage.TextBody = "The following file has been created: " & sNewFileName
'objMessage.Send
Loop
Next
Wend


Sign In
Create Account


Back to top









