Sign in to follow this  
Followers 0
khanhzler

Check File Date/Time

5 posts in this topic

Hi all,

I need to write a script which shall do the following:

a) Watch folders for new files

:) compare creation date/time of the files with the current date/time

c) if the difference is > 5 minutes than popup a msgbox and play a sound and wait for user action

- it would be greate if the script can watch multiple folders

- all variables should be set able eg. path, polling interval, difference

- GUI interface would be nice

Why this script:

Files are created in a Temp folder and will be move over the network.

For some reason, sometimes the move job failed and the files remain in the Temp folder.

So I need a Sript that watch the folder and give me an alarm.

Hopefully somebody can help me out!

Thank you in advance

Kind Regards

Share this post


Link to post
Share on other sites



Look at FileGetTime() in the help file


[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

khanhzler

Hello! Example, watching files every 1 minute:

#include <GuiConstants.au3>
#include <GuiListView.au3>
#include <File.au3>
#include <Date.au3>

Global $SelectFolder, $Timer = 60000, $Hour, $Mins, $Secs

$hGui = GUICreate("Files watcher", 420, 300)

$MainMenu = GUICtrlCreateMenu("&File")
$FolderItem = GUICtrlCreateMenuItem("Select folder", $MainMenu)
GUICtrlCreateMenuItem("", $MainMenu)
$ExitItem = GUICtrlCreateMenuItem("Exit", $MainMenu)

$hListView = _GuiCTrlListView_Create($hGui, "Files|Creation date", 10, 30, 400, 210, $LVS_REPORT, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_CHECKBOXES, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_INFOTIP))

_GUICtrlListView_SetColumnWidth($hListView, 0, 280)
_GUICtrlListView_SetColumnWidth($hListView, 1, 115)

$StartButton = GUICtrlCreateButton("Start watching", 10, 250, 85, 25)

$DelButton = GUICtrlCreateButton("Delete selected", 170, 250, 85, 25)

$StopButton = GUICtrlCreateButton("Stop watching", 326, 250, 85, 25)

$InfoLabel = GUICtrlCreateLabel("Watching folder: None", 10, 10, 400, 17)
GUICtrlSetFont(-1, 8.5, 800)

$TimerLabel = GUICtrlCreateLabel("00:00:00", 365, 10, 50, 16)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE, $ExitItem
        ExitLoop
    Case $FolderItem
        $SelectFolder = FileSelectFolder("Select folder", "")
        If Not @error Then
            GUICtrlSetData($InfoLabel, "Watching folder: " & FileGetShortName($SelectFolder))
        EndIf
        
    Case $StartButton
        If StringRegExp(GUICtrlRead($InfoLabel), ": None$", 0) Then
            MsgBox(16, "Error", "Please select folder")
        Else
            GUICtrlSetState($StartButton, $GUI_DISABLE)
            AdlibEnable("_CheckFiles", 1000)
        EndIf
    Case $DelButton
        _DelItem()
    Case $StopButton
        AdlibDisable()
        GUICtrlSetState($StartButton, $GUI_ENABLE)
        GUICtrlSetData($TimerLabel, "00:00:00")
    EndSwitch
WEnd

Func _CheckFiles()
    If $Timer <= 0 Then
        $Timer = 60000
        _AddFiles($SelectFolder)
    EndIf
    
    $Timer -= 1000
    _TicksToTime(Int($Timer), $Hour, $Mins, $Secs)
    GUICtrlSetData($TimerLabel, StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs))
    
EndFunc

Func _AddFiles($sPath)
    Local $aFolders, $iFullName, $sRet, $iIndex
    
    $aFolders = _FileListToArray($sPath, "*.*")
    If @error Then Return
    
    For $i = 1 To $aFolders[0]
        $iFullName = $sPath &"\"& $aFolders[$i]
        If StringInStr(FileGetAttrib($iFullName), "D") Then
            $sRet = _AddFiles($iFullName)
        Else
            If (_GUICtrlListView_FindText($hListView, $iFullName, -1, False) = -1) _ 
                And (FileGetTime($iFullName, 1, 1) > _NowCalc()) Then
                $iIndex = _GUICtrlListView_AddItem($hListView, $sPath &"\"& $aFolders[$i])
                SoundPlay(@WindowsDir & "\Media\ding.wav")
                Local $aTime = FileGetTime($iFullName, 1)
                _GUICtrlListView_AddSubItem($hListView, $iIndex, StringFormat("%02i.%02i.%04i %02i:%02i:%02i", $aTime[2], $aTime[1], $aTime[0], $aTime[3], $aTime[4], $aTime[5]), 1)
            EndIf
        EndIf
    Next
EndFunc

Func _DelItem()
    Local $iItemCount
    
    $iItemCount = _GUICtrlListView_GetItemCount($hListView)
    If $iItemCount = 0 Then Return
    
    For $i = $iItemCount - 1 To 0 Step -1
        If _GUICtrlListView_GetItemChecked($hListView, $i) Then
            FileDelete(_GUICtrlListView_GetItemText($hListView, $i))
            _GUICtrlListView_DeleteItem($hListView, $i)
            
        EndIf
    Next
EndFunc

If this helped and interesting for you, then inform me about details. :)

Share this post


Link to post
Share on other sites

@rasim

You are greate! The script is cool.

Is it possible to watch multiple folders?

A good idea beside playing a sound is to popup a warning message e.g. Warning! File exist more than 3 minutes in $SelectFolder.

For getting the existing time of the file, I think that you have to compare the creation time to the current time.

Share this post


Link to post
Share on other sites

It would actually be a great idea to try and write the script yourself too you know? Comming here and expecting someone to write the script for you is... insane..

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