Sign in to follow this  
Followers 0
beqa

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

26 posts in this topic

hello all.

i need script for the server to monitor directory for the fles

if i add file to this dir it must delete this file after some period.

please help me.

Share this post


Link to post
Share on other sites



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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

as i understand now i need a loop in which beep in the future deleting files will perform on checking.

how i can do it

Share this post


Link to post
Share on other sites

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. ;)


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

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. ;)


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

great, thanks, now only thing left to record file adding date and then delete it after some period.

Share this post


Link to post
Share on other sites

beqa,

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

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

as, i understood i should write filename filedate in the file, and check it.

am i

right?

Share this post


Link to post
Share on other sites

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

oh, it is whole incorrect code.

can i use variables of _FileSysMonActionEvent into other functions.

Share this post


Link to post
Share on other sites

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:


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

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!

Share this post


Link to post
Share on other sites

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

this forum's wicivic editor isn't accessible for screenreaders and i can't find it always.

Share this post


Link to post
Share on other sites

beqa,

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

[code=autoit:0]

Your code goes here

[/code]
M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

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