0

I have to send email immediately to all users when user upload files in to the folder. My existing code works as follows: we have the Task Scheduler which will run daily at 11am and the files will filter modified date. My aim is to send email when user upload files but cannot run the Scheduler for 1 hour or for every 5 min because the modified date is file created date not the upload date. In the folder below is the code can anyone help me to update the code so that users can receive immediately.

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")

Dim strPath, yr, mnt
yr  = CStr(Year(Now))
mnt = CStr(Month(Now))
strPath = "c:\\users\upload files\email " + yr

Dim strContent 
strContent = ""
Set objFolder = objFso.GetFolder(strPath)

For Each objFile In objFolder.Files
    If objFso.GetExtensionName (objFile.Path) = "pdf" Then
        If objFile.DateLastModified > dateadd("hr", -24, Now) Then
            strContent = strContent + "<li>" + _
                "<a href=""C://Users/uploadfiles/email/" + yr + "/" + _
                objFile.Name + """>" + objFile.Name + "</a></li>"
            MsgBox(strContent)
        End If
    End If
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
sai
  • 97
  • 9
  • Your code is incomplete and doesn't even attempt to send an email. SO has lots of examples for sending mail from VBScript. Please try it yourself first. We're not here to do your work for you. – Ansgar Wiechers Jun 20 '16 at 12:56
  • Hi @AnsgarWiechers there is a code for sending email i just want to pick the fles immediately when user upload – sai Jun 20 '16 at 12:58

1 Answers1

1
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""c:\\\\scripts""'")
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop

This monitors c:\scripts (note backslashes must be doubled then doubled again) for files being created every 10 secs (the within 10).

Set emailObj      = CreateObject("CDO.Message")
emailObj.From     = "dc@gmail.com"

emailObj.To       = "dc@gmail.com"

emailObj.Subject  = "Test CDO"
emailObj.TextBody = "Test CDO"

emailObj.AddAttachment "c:\windows\win.ini"

Set emailConfig = emailObj.Configuration

emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")    = 2  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl")      = true 
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername")    = "Username"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword")    = "Password"
emailConfig.Fields.Update

emailObj.Send

If err.number = 0 then Msgbox "Done"
  • Hi can you please tell why we need impersonate here ? will it run in auto or it should run manually ?? – sai Jun 20 '16 at 12:55
  • Impersonate is the default so you can omit. It means act as you with your security permissions. –  Jun 20 '16 at 13:04
  • There is really two choices impersonate you or be anonymous. Almost nothing will work as anonymous. Because WMI is already running it has it's own security context (which you can't use). So it impersonates you so you can do what you are allowed to do. –  Jun 20 '16 at 13:09
  • will it helps to run the script in auto or need to start manually ?? – sai Jun 20 '16 at 13:10
  • Start it how you want. It runs continuously popping up the filename ADDED to the folder with in 10 secs of it happening. And then waits for the next new file. To stop it use Task Manager. –  Jun 20 '16 at 13:13