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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Monitoring Folder Contents

Status
Not open for further replies.

NickC111

Technical User
Sep 23, 2010
44
0
0
GB
Good Morning

I have been using the following code on checking a folder contents and emailing the results to a particular email

Code:
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

I get the following error message watch.vbs(19, 1) Microsoft VBScript runtime error: Type mismatch: '[string: ""]'

Can anyone help me with this error?

Thanks
 
What is the value of GetINIString("Main", "FileSizeThreshold", "", ".\watch.ini") ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The values are the following

FileSizeThreshold="200"
DontSendFilesLargerThan="400"

Thanks
 
The values are the following

FileSizeThreshold="200"
DontSendFilesLargerThan="400"

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top