File monitoring

Discussion in 'Scripting' started by John, Jan 6, 2009.

  1. John

    John Guest

    Hello everyone,

    I've a a script that every hour move files inside several directories to a
    diferent server that is outside of our newtwork, this is done with a schedule
    batch file that runs every 30 minutes. The problem is that sometimes this
    process crashes and the files are not transferred, generally this is only
    reported when users start to complain about that because their files are not
    where should be.

    So, I have to create a script that searches between several directories (5
    directories to be exact) and look at the files inside, if those files where
    created, for example "3 hours ago", that means that those files should
    already be transferred to the other server, if they're in that directory,
    that means that there is some problem in the server and someone should take a
    look at that.

    In sum, I need a batch file that runs every hour and search across those
    directories for files that were created 3 or mor hours ago, if there is files
    in those conditions, then the batch should send a warning mail to a specific
    mailbox containing the path of the directory and the server name.

    I'm new to scripting, can anyone help me on this one?

    Thank you guys for your time.
    BTW: I'm learning a lot with the great job that people do here.
    John, Jan 6, 2009
    1. Advertisements

  2. Your first job should really be to find out why the "move" process fails on
    occasion. In this way you could treat the cause of your problem instead of
    the symptoms. If this is not possible then you could use the script below.
    While it is probably possible to write a batch file as per your request,
    this would be a painful exercise because batch files do not have native
    functions for date caclulations.

    Pay attention to separate your folder names in Line 02 with vertical bars,
    not spaces.

    01. iInterval = 3 'hours
    02. aFolders = Split("c:\Folder1|d:\Folder 2|c:\Some Folder", "|")
    04. LF = Chr(10)
    05. Set oFSO = CreateObject("Scripting.FileSystemObject")
    06. ReDim aFileCount(UBound(afolders))
    07. For i = 0 To UBound(aFolders)
    08. aFileCount(i) = 0
    09. Next
    11. bFound = False
    12. For i = 0 To UBound(aFolders)
    13. Set oFiles = oFSO.GetFolder(aFolders(i)).Files
    14. For Each oFile In oFiles
    15. If DateDiff("h", oFile.DateLastModified, Now()) > iInterval Then
    16. bFound = True
    17. aFileCount(i) = aFileCount(i) + 1
    18. End If
    19. Next
    20. Next
    21. if not bFound then WScript.Quit
    23. schema = ""
    24. Set objEmail = CreateObject("CDO.Message")
    25. With objEmail
    26. .From = ""
    27. .To = ""
    28. .Subject = "Alert - some old files were found"
    29. .TextBody = ""
    30. For i = 0 To UBound(aFolders)
    31. .TextBody = .TextBody & Right(" " & aFileCount(i), 4) _
    32. & " " & aFolders(i) & LF
    33. Next
    34. '.AddAttachment "d:\Testfile.txt"
    35. WScript.Echo .textbody
    36. With .Configuration.Fields
    37. .Item (schema & "sendusing") = 2
    38. .Item (schema & "smtpserver") = ""
    39. .Item (schema & "smtpserverport") = 25
    40. .Item (schema & "smtpauthenticate") = cdoBasic
    41. .Item (schema & "sendusername") = ""
    42. .Item (schema & "sendpassword") = "SomePassword"
    43. End With
    44. .Configuration.Fields.Update
    45. .Send
    46. End With
    Pegasus \(MVP\), Jan 6, 2009
    1. Advertisements

  3. John

    John Guest

    Hi Pegasus,

    Thank you for your answer. In fact we know what causes our script to
    sometimes break, but we can't do anything at the moment, so we need an
    alternative solution to monitor that folder.

    I did a test your script and everything works great, EXCEPT that I never
    receive the warning mail because the bFound variable always returns false,
    can you help me on that?
    John, Jan 7, 2009
  4. John

    Al Dunbar Guest

    The statement:

    if not bFound then WScript.Quit

    should force an exit before the email-sending code if bFound is false (i.e.
    if "not bFound" is true). Either you have no files older than 3 hours in
    those folders, or DateDiff is returning a negative value. I can never
    remember which of the two dates is subtracted from the other, so would
    suggest changing this:

    If DateDiff("h", oFile.DateLastModified, Now()) > iInterval Then

    to this:

    If abs(DateDiff("h", oFile.DateLastModified, Now())) > iInterval Then

    Al Dunbar, Jan 7, 2009
  5. Insert Line 14a. as follows:
    wscript.echo oFile.Name & " " & DateDiff("h", oFile.DateLastModified, Now())

    You will now see the age of each file on the screen, provided you run the
    command in a Command Prompt and not from the Run box.
    Pegasus \(MVP\), Jan 7, 2009
  6. John

    John Guest

    Thank you both for your support, everything is working very well...
    You're great.

    Best regards.
    John, Jan 8, 2009
  7. Thanks for the feedback. It would be interesting to know why the program did
    not work initially!
    Pegasus \(MVP\), Jan 8, 2009
  8. John

    John Guest

    Hi Pegasus,

    Sorry for the late response. Actually the script does not seem to have
    problems, when I copy it for the second time to do a test in a diferent
    server the script simply worked :D. So I guess that probably the mistake was
    mine when copying those lines.

    What is your opinion on Al's suggestion? Should I make the change?

    I'm thinking in conver it to an exe file, do you recommend any software to
    do this? Will the users be able to revert it back to vbs using anyother

    Thank you again.
    John, Jan 13, 2009
  9. Al's suggestion gets around the human problem of not always remembering
    which way the DateDiff function works. He is in good company with this
    problem . . . His proposal turns all negative numbers into positive ones,
    thus making it irrelevant which argument comes first in the DateDiff
    function. Since your code works (which does not surprise me because I tested
    it before posting!), making the change will make no difference. You can
    therefore do as you please.
    Pegasus \(MVP\), Jan 13, 2009
  10. John

    Allan Miller Guest

    I know some time has passed on this one but I think you would better benefit
    to use WMI to monitor File events.

    Read this post from the Scripting Guy and understand how it works.

    Before you do your file xfer do a Err.Clear so that there are no errors
    known and after your xfer, test the Err.Number. If the Err.Number is not = 0
    then you know something is not going right and you can send yourself a

    Allan Miller, Jan 23, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.