Sign in to follow this  
Followers 0
TonyP

Script to check folders for new files

3 posts in this topic

#1 ·  Posted (edited)

Well I figured this script is too nice to not share. Special thanks to erik7426 who wrote, most of it and to Jos for the email UDF. Actually the only thing I really did was add the email portion, the log files and make it so that it can read from an outside ini file to get the folder paths.

Its been a great array learning experience for me.

#include <Date.au3>
#include <Array.au3>
#include <File.au3>
#include <Email.au3>

;Reads the information in the backup ini file and outputs it into the data variable
$data = IniRead("myfile.ini", "Folders", "Paths", "NotFound")
; The data in the data variable will be split into an array everywhere "," is encountered
$fldr = StringSplit($data, ",")

;Most important piece, it goes through all the strings one by one and runs the EvaluateFiles function on the folder specified in the string
For $i = 1 to $fldr[0]
    EvaluateFiles($fldr[$i])
Next

Func EvaluateFiles($Folder)
    $avFiles = _FileListToArray($Folder & "\", "*",1)
    If @Error<>0 Then
        Return
    EndIf
    
    $iNewestTime = 11111111111111; YYYYMMDDhhmmss
    $iNewestIndex = 0; Array index of newest file
; Find the newest file
    For $p = 1 To $avFiles[0]
        $iFileTime2 = Number(FileGetTime($Folder & "\" & $avFiles[$p], 0, 1))
        If $iFileTime2 > $iNewestTime Then
            $iNewestTime = $iFileTime2
            $iNewestIndex = $p
        EndIf
    Next
    
    If $iNewestIndex > 0 Then
        $t = FileGetTime($Folder & "\" & $avFiles[$iNewestIndex])
        $iDateCalc = _DateDiff( 'd',$t[0] & "/" & $t[1] & "/" & $t[2] & " " & $t[3] & ":" & $t[4] & ":" & $t[5],_NowCalc())
        If $iDateCalc > 0 Then;<---Flags files that are older than today
            $file = FileOpen("C:\CheckBackupsLog.log", 9)
            FileWrite($file, _DateTimeFormat( _NowCalc(),0) & "| " & "The files in - " & $Folder & " - have not been updated today." & @CRLF & $avFiles[$iNewestIndex] & " is the newest file with a modified date of " & $t[1] & "/" & $t[2] & "/" & $t[0] & " " & $t[3] & ":" & $t[4] & ":" & $t[5] & @CRLF & @CRLF)
            FileClose($file)
            
            ;The next section calls a function from the email UDF and sends a email to the email address specified below with a subject of the failed folder and how new the newest file in that folder is
            Global $oMyRet[2]
            Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
            _INetSmtpMailCom("server", "from name", "from email address", "to email address", $Folder & " failed backup!", "The files in - " & $Folder & " - have not been updated today." & @CRLF & @CRLF & $avFiles[$iNewestIndex] & " is the newest file with a modified date of" & @CRLF & @CRLF & $t[1] & "/" & $t[2] & "/" & $t[0] & " " & $t[3] & ":" & $t[4] & ":" & $t[5], "" ,"" , "","High","")
            If @error then
                $file = FileOpen("C:\CheckBackupsErrors.log", 9)
                FileWrite($file, _DateTimeFormat( _NowCalc(),0) & "|" & "Could not send email." & @CRLF) ;Writes a error entry to the log if an email cannot be sent
                FileClose($file)
            EndIf
            
        EndIf
    Else
        $file = FileOpen("C:\CheckBackupsErrors.log", 9)
        FileWrite($file, _DateTimeFormat( _NowCalc(),0) & "|" & "Could not find a file." & @CRLF) ;Writes to error log if there is no file in the folder
        FileClose($file)
    EndIf

EndFunc

Ini file:

CODE
Ini file to specify backup locations to check that there is a recent backup file.

Note: This file must be in the same location as backupcheck.exe

[Folders]

Folder path in the following format: "C:\folder\folder," with no spaces after the comma

Must all be on the same line.

Paths= C:\temp\vmware,C:\temp\stuff,C:\temp\project,C:\tools

Edited by xenon2050

Share this post


Link to post
Share on other sites



This could be quite useful xenon2050.

With a few changes I think it would be good for helping to develop scripts for web sites. Suppose you use an an editor like SciTE to write a php or html file, then every time you save changes to the file this function could upload it to your website using ftp, and all you need to do is press refresh on your browser to see it work. If you don't have a web server on your PC then the easiest way to test scripts which use php for example is to upload them. (Or at least that is what I have found but I've only just started trying to learn php.)


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Hmm, that is a interesting idea. Even when you do have a webserver on your computer something like that could be rather useful. I'll have to play around with the script a bit and see if I could add some functionality like that.

I use php for my two websites and I think you got it right; besides having a webserver running locally you have to upload the files. I use XAMPP for my web server needs. With that I can code in PHP without needing to be connected to the internet.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0