I have written a little vbs file that monitors a directory and if a file has been in that directory for over 4 hours it moves it to another directory and then emails. This works perfectly fine at my home behind my cable modem and router on a windows xp pro machine. Here is the code
Option Explicit
On Error Resume Next
Dim fso, FileSet, Path, File, DDiff, Date1, Date2, DestPath, objEmail
Do
Path = "C:\test" 'Directory that the script is monitoring
DestPath = "C:\TestArchive\" 'Directory that a file will get moved to
FileSet = GetDirContents(Path) 'Call the GetDirContents Function
For each File in FileSet
Set File = fso.GetFile(Path & "\" & File)
Date1 = File.DateLastModified
'date is in 24 hours spands, so if you want days i day is 24
Date2 = Now()
DDiff = Abs(DateDiff("h", Date1, Date2))
If DDiff >= 4 Then
If Not fso.FileExists(DestPath & File.Name) Then
File.Move DestPath
Set objEmail = CreateObject("CDO.Message"
)
objEmail.From = "sender@emaildomain.com" 'Put your own from email address
objEmail.To = "receiver@emaildomain.com"
'Put your own email to address
objEmail.Subject = "Email Subject" 'Email subject line
objEmail.Textbody = "File, [" & File.Name & "], has been moved to the Import_Archive directory" 'What you want the email to say
objEmail.Configuration.Fie
lds.Item ("
http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fie
lds.Item ("
http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.domain.net" 'change to your own smtp
objEmail.Configuration.Fie
lds.Item ("
http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fie
lds.Update
objEmail.Send
'wscript.echo "Finished [" & File.Name & "] Now" 'This was just for testing purposes
'wscript.echo File.Name 'This was just for testing purposes
Else
wscript.echo "Unable to move file [" & File.Name & "]. A file by this name already exists in the target directory."
End If
End If
Next
'wscript.echo "Finished [" & File.Name & "] Now"
Wscript.Sleep 60000 'Pause for 10 minutes
Loop 'Loop back up to the Do statement and start over after the 10 minute pause
Function GetDirContents(FolderPath)
Dim FileCollection, aTmp(), i
Set fso = CreateObject("Scripting.Fi
leSystemOb
ject")
Set FileCollection = fso.GetFolder(FolderPath).
Files
Redim aTmp(FileCollection.count - 1)
i = -1
For Each File in FileCollection
i = i + 1
aTmp(i) = File.Name
Next
GetDirContents = aTmp
End Function
If I use this same code behind a network and put in their smtp server mail.thierdomain.org it doesn't work. (it will move the files but never emails) This is on a 2003 server when on the network. I know the smtp server is correct for this network, but what could be my other problems? thanks.
Start Free Trial