Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

watch FTP folder for files older then 30min and email list of files

Status
Not open for further replies.

pflaker

Technical User
Aug 26, 2011
2
US
please note that i am by no means a programmer of any kind i just have a knack for figuring things out.

I have a script that works great and emails me files that are larger then x in bytes but i would like to change it to be for time in minutes. this is not mine i found it and it has been working great for me so far but i need to change it now.

below is the script i am using. any help would be great.

'FolderMonitor.vbs - 1.0a, 10/01/10
'
'Rob Dunn
'
'I put this together a few years ago, and updated it today for a user
' on the Spiceworks forums.
'
'This script will check a folder (share) and email you if any files in the
' folder are larger than the byte size designated in the watch.ini file.
' If so, it will attach any files to the email if blnAttachFiles = true and
' send it to the recipient listed in the 'SendTo' INI value.
'
' Reads INI file for settings - here's the format (name it 'watch.ini' in the same folder):
'
'By the way - FileSizeThreshold number is in byte format

'[Main]
'WatchServers="server"
'WatchDirectory="\sharename"
'FileSizeThreshold="200"
'DontSendFilesLargerThan="400"
'
'
'SMTPServer="x.x.x.x"
'SendFrom="folderwatcher_noreply@yourdomain.com"
'SendTo="you@yourdomain.com"
'Subject="Folder Contents"
'CC=""

Dim blnAttachFiles, iDontSendFilesLargerThan, sMessage

Const ForReading = 1
Const cdoSendUsingMethod = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing",[/URL] _
cdoSendUsingPort = 2, _
cdoSMTPServer = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver"[/URL]

'// Create the CDO connections.
Dim iMsg, iConf, Flds, blnSendMail, debug, strDate

debug = 0

blnAttachFiles = true
blnSendMail = false

strWatchDir = GetINIString("Main", "WatchDirectory", "", ".\watch.ini")
strWatchServers = GetINIString("Main", "WatchServers", "", ".\watch.ini")
strCompare = GetINIString("Main", "Comparison", "", ".\watch.ini")
strFileSize = Int(GetINIString("Main", "FileSizeThreshold", "", ".\watch.ini"))
iDontSendFilesLargerThan = Int(GetINIString("Main", "DontSendFilesLargerThan", "", ".\watch.ini"))
strSMTPServer = GetINIString("Email", "SMTPServer", "", ".\watch.ini")
strSendTo = GetINIString("Email", "SendTo", "", ".\watch.ini")
strSendFrom = GetINIString("Email", "SendFrom", "", ".\watch.ini")
strSubject = GetINIString("Email", "Subject", "", ".\watch.ini")


'If debug = 0 Then wscript.echo strWatchServers & vbcrlf & strWatchDir & vbcrlf & strFileSize _
' & vbcrlf & strSMTPServer & vbcrlf & strSendTo & vbcrlf _
' & strSendFrom & vbcrlf & strSubject & vbcrlf & strCompare


strWatchServers = split(strWatchServers,",", -1,1)
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields

'// SMTP server configuration.
With Flds
.Item(cdoSendUsingMethod) = cdoSendUsingPort

'// Set the SMTP server address here.
.Item(cdoSMTPServer) = strSMTPServer
.Update
End With

For i=0 to UBound(strWatchServers)
Set iMsg = CreateObject("CDO.Message")

If debug = 1 Then wscript.echo strWatchServers(i)

strWatchDir_a = "\\" & strWatchServers(i) & strWatchDir



'// Set the message properties.
With iMsg
Set .Configuration = iConf
.To = strSendTo
.From = strSendFrom
.Subject = "Contents of " & strWatchDir_a
.TextBody = strSubject & " - " & strWatchDir_a & " - " & Now
End With
If debug = 1 Then wscript.echo strWatchDir_a
Call ShowFolderList(strWatchDir_a)

Next

Function AddAttachment(strFile)
'// An attachment can be included.
iMsg.AddAttachment strFile
End Function

Function SendMail
'// Send the message.
iMsg.TextBody = iMsg.TextBody & vbcrlf & vbcrlf & sMessage
iMsg.Send ' send the message.
Set imsg = nothing
End Function

Function ShowFolderList(folderspec)
Dim fso, f, f1, fc, s

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)

Set fc = f.Files
For Each f1 in fc
if debug = 1 then wscript.echo f1.name & vbtab & f1.size
If f1.size > strFileSize Then
blnSendMail = true
s = s & f1.name & vbtab & f1.size
s = s & vbcrlf
'fso.CopyFile f1.path, folderspec & "\" & strDate & "\" & f1.name
If blnAttachFiles = true and f1.size < iDontSendFilesLargerThan then
Call AddAttachment(f1.path)
End If

sMessage = sMessage & f1.name & vbtab & f1.size & " bytes" & vbnewline
End If

if debug = 1 then wscript.echo sMessage
Next


ShowFolderList = s
If debug = 1 Then wscript.echo s

If CStr(err.number) <> 0 Then
Call ErrorHandle(folderspec)
ElseIf blnSendMail = True Then
Call SendMail
End If

blnSendMail = false

End Function

Sub ErrorHandle(folderspec)
WshShell.Popup "Error: Path on " & folderspec & " does not exist or no files " _
& "were found.", 7, "No files found in " & folderspec, 64
End Sub

Sub WriteINIString(Section, KeyName, Value, FileName)
Dim INIContents, PosSection, PosEndSection

'Get contents of the INI file As a string
INIContents = GetFile(FileName)

'Find section
PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
If PosSection>0 Then
'Section exists. Find end of section
PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
'?Is this last section?
If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1

'Separate section contents
Dim OldsContents, NewsContents, Line
Dim sKeyName, Found
OldsContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
OldsContents = split(OldsContents, vbCrLf)

'Temp variable To find a Key
sKeyName = LCase(KeyName & "=")

'Enumerate section lines
For Each Line In OldsContents
If LCase(Left(Line, Len(sKeyName))) = sKeyName Then
Line = KeyName & "=" & Value
Found = True
End If
NewsContents = NewsContents & Line & vbCrLf
Next

If isempty(Found) Then
'key Not found - add it at the end of section
NewsContents = NewsContents & KeyName & "=" & Value
Else
'remove last vbCrLf - the vbCrLf is at PosEndSection
NewsContents = Left(NewsContents, Len(NewsContents) - 2)
End If

'Combine pre-section, new section And post-section data.
INIContents = Left(INIContents, PosSection-1) & _
NewsContents & Mid(INIContents, PosEndSection)
else'if PosSection>0 Then
'Section Not found. Add section data at the end of file contents.
If Right(INIContents, 2) <> vbCrLf And Len(INIContents)>0 Then
INIContents = INIContents & vbCrLf
End If
INIContents = INIContents & "[" & Section & "]" & vbCrLf & _
KeyName & "=" & Value
end if'if PosSection>0 Then
WriteFile FileName, INIContents
End Sub


Function GetINIString(Section, KeyName, Default, FileName)
Dim INIContents, PosSection, PosEndSection, sContents, Value, Found
'Get contents of the INI file As a string
INIContents = GetFile(FileName)
'Find section
PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)

If PosSection > 0 Then
'Section exists. Find end of section

PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
'?Is this last section?
If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1

'Separate section contents
sContents = Mid(INIContents, PosSection, PosEndSection - PosSection)

If InStr(1, sContents, vbCrLf & KeyName & "=", vbTextCompare)>0 Then
Found = True
'Separate value of a key.
Value = SeparateField(sContents, vbCrLf & KeyName & "=", vbCrLf)
End If
End If
If isempty(Found) Then Value = Default

GetINIString = replace(Value,Chr(34),"")
End Function

'Separates one field between sStart And sEnd
Function SeparateField(ByVal sFrom, ByVal sStart, ByVal sEnd)
Dim PosB: PosB = InStr(1, sFrom, sStart, 1)
If PosB > 0 Then
PosB = PosB + Len(sStart)
Dim PosE: PosE = InStr(PosB, sFrom, sEnd, 1)
If PosE = 0 Then PosE = InStr(PosB, sFrom, vbCrLf, 1)
If PosE = 0 Then PosE = Len(sFrom) + 1
SeparateField = Mid(sFrom, PosB, PosE - PosB)
End If
End Function

'File functions
Function GetFile(ByVal FileName)
Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
'Go To windows folder If full path Not specified.
If InStr(FileName, ":\") = 0 And Left (FileName,2) <> "\\" And Left (FileName,2) <> ".\" Then
FileName = FS.GetSpecialFolder(0) & "\" & FileName
End If
'On Error Resume Next
End Function

'Separates one field between sStart And sEnd
Function SeparateField(ByVal sFrom, ByVal sStart, ByVal sEnd)
Dim PosB: PosB = InStr(1, sFrom, sStart, 1)
If PosB > 0 Then
PosB = PosB + Len(sStart)
Dim PosE: PosE = InStr(PosB, sFrom, sEnd, 1)
If PosE = 0 Then PosE = InStr(PosB, sFrom, vbCrLf, 1)
If PosE = 0 Then PosE = Len(sFrom) + 1
SeparateField = Mid(sFrom, PosB, PosE - PosB)
End If
End Function

'File functions
Function GetFile(ByVal FileName)

Set FS = CreateObject("Scripting.FileSystemObject")

'Go To windows folder If full path Not specified.
If InStr(FileName, ":\") = 0 And Left(FileName,2)<> "\\" And Left(FileName,2) <> ".\" Then
FileName = FS.GetSpecialFolder(0) & "\" & FileName
End If
On Error Resume Next

GetFile = FS.OpenTextFile(FileName).ReadAll
'wscript.echo getfile
End Function

Sub WriteINIStringVirtual(Section, KeyName, Value, FileName)
WriteINIString Section, KeyName, Value, _
Server.MapPath(FileName)
End Sub

Function GetINIStringVirtual(Section, KeyName, Default, FileName)
GetINIStringVirtual = GetINIString(Section, KeyName, Default, _
Server.MapPath(FileName))
End Function

Function MakeSureDirectoryTreeExists(dirName)
Dim aFolders, newFolder
If debug = 1 Then wscript.echo "Makesuredirectorytreexists " & dirname
dim delim
' Creates the FSO object.
Set fso = CreateObject("Scripting.FileSystemObject")

' Checks the folder's existence.
If Not fso.FolderExists(dirName) Then

' Splits the various components of the folder name.
If instr(dirname,"\\") then
delim = "-_-_-_-"
dirname = replace(dirname,"\\",delim)
'wscript.echo dirname
End if
aFolders = split(dirName, "\")
if instr(dirname,delim) Then
dirname = replace(aFolders(0),delim,"\\")
'wscript.echo "aFolders = " & dirname
End if
' Obtains the drive's root folder.

newFolder = fso.BuildPath(dirname, "\")

' Scans each component in the array, and create the appropriate folder.
For i=1 to UBound(aFolders)
newFolder = fso.BuildPath(newFolder, aFolders(i))

If Not fso.FolderExists(newFolder) Then

fso.CreateFolder newFolder


End If
Next
End If
End Function
 
Quick fix.
In function ShowFolderList, modify the if that checks size to check date instead

Code:
If f1.size > strFileSize Then

would become something like

Code:
If datediff("n", f1.datecreated, now) > 30 Then

Long and preferred fix. (for the next guy's sake)
In the watch.INI file Main section, add a line for [red]FileAgeInMintutesThreshold=30[/red]. In the code, add intAge = Int(GetINIString("Main", "FileAgeInMintutesThreshold", "", ".\watch.ini")). Modify the test condition to
Code:
If datediff("n", f1.datecreated, now) > intAge Then

-Geates




"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
I'm glad it was a valuable post. Now, back to my real job :)

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top