0

I have written a script to check that 4 files exist in a folder. I need to use the below error handling for this particular small piece of code.

This code checks various folders to see if it contains 4 files. If it does then good if it doesn't then its not good.

Code:

Const intOK = 0
Const intCritical = 2
Const intError = 3
Const ForReading=1,ForWriting=2,ForAppending=8
Dim filename,filenamemov,emailaddr
Dim arrFolders(17)
Dim argcountcommand,dteToday
Dim arg(4)
Dim strMailServer,Verbose,strExt,numfiles,intIndex
Dim intcount : intCount = 0
Dim stroutput

numfiles =  4  'how many minutes old are the files
Verbose = 0    '1 FOR DEBUG MODE, 0 FOR SILENT MODE

arrFolders(0) = "D:\AS2\Inbound\WESSEX"
arrFolders(1) = "D:\AS2\Inbound\EATWELL"
arrFolders(2) = "D:\AS2\Inbound\TURNER\"

For intIndex = 0 To UBound(arrFolders)
    pt "Checking folder: " & arrFolders(intIndex)
    If arrFolders(intIndex) = "" Then
        pt "Empty folder value!"
        Exit For
    Else
        Call checkfiles(arrFolders(intIndex))
    End If
Next

If objFolder.Files.Count < 4 Then
    WScript.Echo "CRITICAL - " & intCount & " File(s) over " & numfiles & " 
minutes in " & stroutput
    WScript.Quit(intCritical)
Else
    WScript.Echo  "OK - No directory contains less than 4 files"
    WScript.Quit(intOK)
End If

Sub checkfiles(folderspec)
    'check If any files exist on folder
    On Error Resume Next
    Dim objFso : Set objFso = CreateObject("Scripting.FileSystemObject")

    'you can take this as input too using InputBox 
    'this will error If less than 4 files exist.
    Dim objFolder : Set objFolder = objFso.GetFolder(strFolderPath) 
    If objfolder.Files.Count = 4 Then
        MsgBox "This is Correct"
    Else
        MsgBox "This isnt Correct"
    End If

    Sub pt(txt)
        If Verbose = 1 Then
            WScript.Echo txt
        End If
    End Sub
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
George H
  • 31
  • 6
  • What exactly is the issue in your code? If you just want to get your code reviewed, you should use https://codereview.stackexchange.com/ – Pankaj Jaju Aug 13 '18 at 15:18
  • sorry for not explaining correctly. I want this code to check that 4 files exist in a folder. This does not do this at the moment and I get no error. Was wondering if there was a flaw somewhere as I can not spot it. – George H Aug 13 '18 at 15:27
  • As I am using this as a plugin to be managed within Nagios I cant have Msgboxes I was wondering if there was another way to store the result. as you can see if there are not 4 files I want it to echo Critical if not then echo Ok. – George H Aug 13 '18 at 15:29
  • objFso.FileExists method should be an easy way to check for file's existence rather than relying on count and it will also trim lot of coding for you – Pankaj Jaju Aug 13 '18 at 15:30
  • I am not familiar with this method, how could I change the code? – George H Aug 13 '18 at 15:33
  • I need to check if 4 files exist within the same folder – George H Aug 13 '18 at 15:34
  • Its literally objFso.FileExists(filepath). Check this out for sample code --> https://stackoverflow.com/questions/3031497/how-to-verify-if-file-exist-with-vb-script – Pankaj Jaju Aug 13 '18 at 15:46
  • The code you posted is broken and cannot possibly work. The line `WScript.Echo "CRITICAL - " & ...` has a string that is wrapped to the next line (which is invalid syntax in VBScript), and you're missing an `End Sub`. Please create a [mcve] that demonstrates your problem, then copy/paste that code and any errors raised by that code. – Ansgar Wiechers Aug 13 '18 at 16:47

1 Answers1

1

If i understand your question, you want to

  • check a set of folders kept in an array for the number of files they contain
  • if any of the folders has less than 4 files, an error message should be displayed
  • the folder array CAN contain empty values and the routine should skip those

Apparently you have more plans considering all 'extra' variables you have in your code like Const ForReading=1,ForWriting=2,ForAppending=8 and Dim filename,filenamemov,emailaddr, but I left them out here, because they have nothing to do with the question at hand.

Option Explicit

Const intOK = 0
Const intCritical = 2
Const intError = 3

Dim Verbose, path, fileCount, minCount, intCount, errCount
Dim objFso, objFolder, intResult, strResult

Verbose  = 0   '1 FOR DEBUG MODE, 0 FOR SILENT MODE
minCount = 4   'The minimal number of files each folder should have

Dim arrFolders(17)
arrFolders(0) = "D:\AS2\Inbound\WESSEX"
arrFolders(1) = "D:\AS2\Inbound\EATWELL"
arrFolders(2) = "D:\AS2\Inbound\TURNER\"

Set objFso = CreateObject("Scripting.FileSystemObject")

intResult = intOK  'assume all is well
strResult = ""     'to accumulate critical errors for each folder
intCount  = 0      'the total running count of folders we have checked
errCount  = 0      'the total count of errors (folders with less than 4 files) we have found
For Each path In arrFolders
    If Len(path) > 0 Then
        intCount = intCount + 1
        WScript.Echo "Checking folder: " & path
        Set objFolder = objFso.GetFolder(path)
        fileCount = objfolder.Files.Count
        'considering the "OK - No directory contains less than 4 files" message
        'in your original post, this test needs to do a 'Greater Than Or Equal To', so use  >=
        If fileCount >= minCount Then
            WScript.Echo "This is correct: " & path
        Else
            WScript.Echo "This is NOT correct: " & path
            strResult = strResult & vbNewLine & "CRITICAL - " & fileCount & " File(s) in folder " & path
            intResult = intCritical
            errCount = errCount + 1
        End If
    End if
Next

'clean up used objects
Set objFolder = Nothing
Set objFso = Nothing

If errCount > 0 Then
    'we have folders with errors
    WScript.Echo strResult
Else
    'This message implies that a folder is also 'Good' when more than 4 files exist
    WScript.Echo "OK - All " &  intCount & " directories contain at least " & minCount & " files"
End If
WScript.Quit(intResult)
Theo
  • 57,719
  • 8
  • 24
  • 41