Jump to content

monitor dir for added files and after adding file delete after soem time


Go to solution Solved by Melba23,

Recommended Posts

  • Moderators

beqa,

I have used the FileSystemMonitor UDF to detect new files being added to a folder - knowing which file was added will enable you to delete it after a specified delay. Give it a try and see how you get on. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

this is what i understood from this udf

 
#include <FileSystemMonitor.au3>

_FileSysMonSetup(1, "C:\", "")

While 1

    _FileSysMonDirEventHandler()

        _FileSysMonSetDirMonPath("C:\1")
WEnd

Func _FileSysMonActionEvent($event_type, $event_id, $event_value)

    Local $event_type_name
    Local $fs_event = ObjCreate("Scripting.Dictionary")

    Switch $event_type
        
        Case 0

            $fs_event.item(Hex(0x00000001)) = "file added to the directory|FILE_ACTION_ADDED"
            beep(100,1000)
            $fs_event.item(Hex(0x00000002)) = "file removed from the directory|FILE_ACTION_REMOVED"             
beep(200,2000)
            $fs_event.item(Hex(0x00000003)) = "file was modified|FILE_ACTION_MODIFIED"
            $fs_event.item(Hex(0x00000004)) = "file was renamed old name|FILE_ACTION_RENAMED_OLD_NAME"
            $fs_event.item(Hex(0x00000005)) = "file was renamed new name|FILE_ACTION_RENAMED_NEW_NAME"
    EndSwitch
Edited by Melba23
Added code tags
Link to comment
Share on other sites

  • Moderators

beqa,

This thread gives much more detail on how to use the UDF and contains a couple of scripts where I rewrote the UDF and example to make them a bit clearer - I hope it will explain things a little better. :)

M23.

P.S. When you post code please use Code tags - see here how to do it. Then you get a scrolling box and syntax colouring as you can see above now I have added the tags. ;)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

hi.

 

please lookat this code and tell if something is wrong.

#include <filesysmon.au3>

_FileSysMonSetup(1, "C:\1")

While 1
_FileSysMonDirEventHandler()
WEnd

Func _FileSysMonActionEvent($event_type, $event_id, $event_value)
msgbox(0, $event_type & " - " & $event_id & " - " & $event_value, "test")
EndFunc
Edited by Melba23
Added code tags
Link to comment
Share on other sites

  • Moderators

beqa,

Looks fine to me. :thumbsup:

If you only want to know when a file has been added to the folder you can do something like this: :)

Func _FileSysMonActionEvent($event_type, $event_id, $event_value)
    Switch $event_type
        Case 0 ; Folder event
            Switch $event_id
                Case 1 ; File added
                    MsgBox(0, "File Added", $event_value & " was added to the folder")
            EndSwitch
    EndSwitch
EndFunc
All good now? :)

M23

P.S. Please use code tags as I suggested earlier. ;)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

beqa,

FileGetTime, _DateDiff & FileDelete should be able to help you there. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

beqa,

I would store the file name and creation time within the script and check the time elapsed since creation every so often - an Adlib function might be a good way to do this. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

please if you can look at this code.

#include <filesysmon.au3>
#include <Date.au3>

_FileSysMonSetup(1, "C:\1")

While 1
_FileSysMonDirEventHandler()
WEnd

Func _FileSysMonActionEvent($event_type, $event_id, $event_value)
Switch $event_type
Case 0 ; Folder event
Switch $event_id
Case 1 ; File added
_checkFile()
AdlibRegister("_checkFile", 10000)
EndSwitch
EndSwitch
EndFunc

Func _checkFile($event_value)
If _IsFileOlder($event_value, 10) Then
MsgBox(4096, '', 'File is older than 10 days.')
Else
MsgBox(4096, '', 'File isn''t older than 10 days.')
EndIf
EndFunc

Func _IsFileOlder($event_value, $iDays)
Local $aArray = FileGetTime($event_value, 0)
Return _DateDiff('D', $aArray[0] & '/' & $aArray[1] & '/' & $aArray[2]

& ' ' & $aArray[3] & ':' & $aArray[4] & ':' & $aArray[5], @YEAR & '/' &

@MON & '/' & @MDAY & ' ' & @HOUR & ':' & @MIN & ':' & @SEC) >= $iDays
EndFunc
 

on line 15 there is an error as i understood i mustcall event_value parameter, but i don't know how.

Edited by Melba23
Added code tags
Link to comment
Share on other sites

  • Moderators
  • Solution

beqa,

I would do it like this: :)

#include "FileSystemMonitor_Mod.au3"
#include <Date.au3>

Global $aFileData[2]
Global $sPath = "C:\1"

_FileSysMonSetup(1, $sPath)

AdlibRegister("_checkFile", 10000)

While 1
    _FileSysMonDirEventHandler()
    Sleep(10) ; Important so as not to fry the CPU <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
WEnd

Func _FileSysMonActionEvent($event_type, $event_id, $event_value)
    Switch $event_type
        Case 0 ; Folder event
            Switch $event_id
                Case 1 ; File added
                    ; Save the data
                    $aFileData[0] = $sPath & "\" & $event_value
                    $aTime = FileGetTime($aFileData[0])
                    $aFileData[1] = $aTime[0] & "/" & $aTime[1] & "/" & $aTime[2] & " " & $aTime[3] & ":" & $aTime[4] & ":" & $aTime[5]
                    ConsoleWrite($aFileData[0] & " - " & $aFileData[1] & @CRLF) ; Just so you can see what is stored
            EndSwitch
    EndSwitch
EndFunc   ;==>_FileSysMonActionEvent

Func _checkFile()

    ; Check a file has been saved
    If $aFileData[0] Then
        ; Get seconds since creation
        $iSecs = _DateDiff("s", $aFileData[1], _NowCalc())
        If $iSecs > 120 Then
            ConsoleWrite("File is over 2 mins old" & @CRLF)
            ; Clear array ready for next file
            Global $aFileData[2]
        Else
            ConsoleWrite("File is " & $iSecs & " secs old" & @CRLF)
        EndIf
    EndIf

EndFunc   ;==>_checkFile
Please ask if you have any questions. :)

M23

P.S. And if you post any more code without using the code tags I will stop helping (use the link in post #5 to see how to do it). :naughty:

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

thanks a lot for help.

 

it works

#include <filesysmon.au3>
#include <Date.au3>

Global $aFileData[2]
Global $sPath = "C:\1"

_FileSysMonSetup(1, $sPath)

AdlibRegister("_checkFile", 3600000)

While 1
_FileSysMonDirEventHandler()
Sleep(10)
WEnd

Func _FileSysMonActionEvent($event_type, $event_id, $event_value)
Switch $event_type
Case 0 ; Folder event
Switch $event_id
Case 1 ; File added
$aFileData[0] = $sPath & "\" & $event_value
$aTime = FileGetTime($aFileData[0])
$aFileData[1] = $aTime[0] & "/" & $aTime[1] & "/" & $aTime[2] & " " & $aTime[3] & ":" & $aTime[4] & ":" & $aTime[5]
EndSwitch
EndSwitch
EndFunc;==>_FileSysMonActionEvent

Func _checkFile()

If $aFileData[0] Then
$iDays = _DateDiff("d", $aFileData[1], _NowCalc())
If $iDays > 10 Then
FileDelete($aFileData[0])
Global $aFileData[2]
Else
msgbox(0, "test", "File is " & $iDays & " secs old" & @CRLF)
EndIf
EndIf

EndFunc;==>_checkFile
now i will fetch variable values from .configuration file Edited by Melba23
Added code tags - for the final time!
Link to comment
Share on other sites

  • Moderators

beqa,

Delighted I could help. :)

But I have a question in return. You obviously understand English - so why are you ignoring my requests to use code tags when you post code? :huh:

Using the tags makes for a much more pleasant reading experience for other forum users - as you can see above in the many posts where I have added them - which is why we encourage their use. So in future please do use them - or I will begin to get annoyed. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

beqa,

Then please add the following before and after the code you post: :)

[code=autoit:0]

Your code goes here

[/code]
M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

nice script Melba,

Just what i was looking for....

but just to clearify..

I tweaked it a little bit because i want to use it for monitoring a directory and get the new file and put it somewhere else..

#include "FileSystemMonitor.au3"
#include <Date.au3>

Global $aFileData[2]
Global $sPath = "C:\1"

_FileSysMonSetup(1, $sPath)

AdlibRegister("_checkFile", 10000)

While 1
    _FileSysMonDirEventHandler()
    Sleep(10)
WEnd

Func _FileSysMonActionEvent($event_type, $event_id, $event_value)
    Switch $event_type
        Case 0
            Switch $event_id
                Case 1

                    $aFileData[0] = $sPath & "\" & $event_value
                    $aTime = FileGetTime($aFileData[0])
                    $aFileData[1] = $aTime[0] & "/" & $aTime[1] & "/" & $aTime[2] & " " & $aTime[3] & ":" & $aTime[4] & ":" & $aTime[5]
                    ConsoleWrite($aFileData[0] & " - " & $aFileData[1] & @CRLF)
            EndSwitch
    EndSwitch
EndFunc

Func _checkFile()


    If $aFileData[0] Then

        $iSecs = _DateDiff("s", $aFileData[1], _NowCalc())
        If $iSecs > 120 Then
            ConsoleWrite("File is over 2 mins old" & @CRLF)
        
              FileCopy( $sPath & "\" $aFileData,"C:\2\"& $aFileData,8); <<<< 
           Global $aFileData[2]
        Else
            ConsoleWrite("File is " & $iSecs & " secs old" & @CRLF)
            
        EndIf
    EndIf

EndFunc

Iff i use filecopy $aFileData it gets the whole directory and i want just that file....

And iff there are more new files instead of only 1 new file I also want to copy them....

Could u help me whit that?

maybe i missed something....

thnx in advanced

Edited by FMS

as finishing touch god created the dutch

Link to comment
Share on other sites

  • Moderators

FMS,

The file name and time are stored in an array - to access them you need to use the correct index. At present you are using just the array name in your FileCopy line, which returns a blank value and so the string is just the path. ;)

Try this code:

#include "FileSystemMonitor.au3"
#include <Date.au3>

Global $aFileData[2]
Global $sPath = "C:\1"

_FileSysMonSetup(1, $sPath)

AdlibRegister("_checkFile", 10000)

While 1
    _FileSysMonDirEventHandler()
    Sleep(10)
WEnd

Func _FileSysMonActionEvent($event_type, $event_id, $event_value)
    Switch $event_type
        Case 0
            Switch $event_id
                Case 1
                    $aFileData[0] = $event_value ; Only save the filename <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                    $aTime = FileGetTime($aFileData[0])
                    $aFileData[1] = $aTime[0] & "/" & $aTime[1] & "/" & $aTime[2] & " " & $aTime[3] & ":" & $aTime[4] & ":" & $aTime[5]
                    ; Which means we need to add the path here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                    ConsoleWrite($spath & "\" & $aFileData[0] & " - " & $aFileData[1] & @CRLF)
            EndSwitch
    EndSwitch
EndFunc

Func _checkFile()
    If $aFileData[0] Then
        $iSecs = _DateDiff("s", $aFileData[1], _NowCalc())
        If $iSecs > 120 Then
            ConsoleWrite("File is over 2 mins old" & @CRLF)
            ; Access the array element containing the filename <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            FileCopy($sPath & "\" & $aFileData[0], "C:\2\" & $aFileData[0], 8)
            Global $aFileData[2]
        Else
            ConsoleWrite("File is " & $iSecs & " secs old" & @CRLF)
        EndIf
    EndIf
EndFunc
That works for me. :)

As for dealing with several files, you would need to declare the $aFileData array as a 2D array and then add each newly created file to the list, removing it once it had been copied. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...