Jump to content

file modification listener


jing
 Share

Recommended Posts

I'm sorry I didn't make myself clear. What I am looking for is a program can detect whether a file has been modified by checking the modification date of the file. The problem now is I don't want to simply use a loop to detect endlessly. I am trying to implement it in a event-driven way so the program can run at the background. And it will be active only the file it monitors has been changed.

Anybody can tell me how to do it?

Edited by jing
Link to comment
Share on other sites

  • Moderators

Now I am trying to do a program which acts as a file modification listener, which means it monitors a specific file and do some successive actions. Because it is more like a program running at the background, I hope it won't use too much resources. It seems event driven is the only way. Anybody can give me a hint how to do it?

I have no idea on what you're asking... Do you?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Well, to answer your question, What you are asking, as far as I can tell reading the helpfile, is only possible with a GUI you made with AutoIt. Anything else you would need to use a loop. All you would have to do is filegettime, and have it saved to a variable. Setup a loop, then have filegettime check the file every so often. Have that saved to a different variable. Do a compare between the two variables. When they do not match, you know it was modified. At this point, have it report to you however you like - msgbox, txt file, or whatever.

Edited by vollyman
Link to comment
Share on other sites

  • Moderators

A suggestion would be to look at 3rd party tools that do MD5/SHA1/or CRC32 checksums. (AutoIt is a tad slow with the current written ones). Maybe some that have command line parameters you can pass. If any of those values change, then the file has been modified.

Other options are:

FileGetSize() (Although the file can be modified without changing its size)

FileGetTime() (Shows the Modification Date and Time (Also Create and or Accessed)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

A suggestion would be to look at 3rd party tools that do MD5/SHA1/or CRC32 checksums. (AutoIt is a tad slow with the current written ones). Maybe some that have command line parameters you can pass. If any of those values change, then the file has been modified.

Other options are:

FileGetSize() (Although the file can be modified without changing its size)

FileGetTime() (Shows the Modification Date and Time (Also Create and or Accessed)

FileGetTime() is good enough for me to use. I just need a way to implement it without a explicit loop 'coz I need run it at the background.

Link to comment
Share on other sites

just put a sleep(500) in the loop, that will keep processor use down around 1-2% the longer the sleep the less usage but the slower the reaction to a file change

That is not a proper solution, right?

Isn't there a way can really solve this?

Link to comment
Share on other sites

  • Moderators

That is not a proper solution, right?

Isn't there a way can really solve this?

What's your question again?

How often do you want to search the Directorie(s)/File(s) to see if there is a change?

AdlibEnable() may be all you need.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

What's your question again?

How often do you want to search the Directorie(s)/File(s) to see if there is a change?

AdlibEnable() may be all you need.

well, my program is supposed to run at the background all the time. It should reacts fast enough when a file has modified. That's why I don't want to use a loop, it just consumes too much resources. Yes, we can set a sleep time, but the longer the interval is, the slower the response might be.

AdlibEnable() is a loop anyway. I will use it if I really can not find any better solution based on event driven way.

Thanks a lot anyway.

Link to comment
Share on other sites

  • Moderators

well, my program is supposed to run at the background all the time. It should reacts fast enough when a file has modified. That's why I don't want to use a loop, it just consumes too much resources. Yes, we can set a sleep time, but the longer the interval is, the slower the response might be.

AdlibEnable() is a loop anyway. I will use it if I really can not find any better solution based on event driven way.

Thanks a lot anyway.

Ok... So... You've ruled out a loop, and now you want a better solution than AdlibEnable(), I'm game for the answer to this one myself.

Oh... And what do you mean "AdlibEnable()" is in a "loop"?

AdlibEnable('_CheckFunction', 10);ever 10 milleseconds (fast enough?)
While 1
    ;Do Nothing
    Sleep(1000000)
WEnd

Func _CheckFunction()
    ;Whatever you are doing here
EndFunc
What's so hard about that?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Ok... So... You've ruled out a loop, and now you want a better solution than AdlibEnable(), I'm game for the answer to this one myself.

Oh... And what do you mean "AdlibEnable()" is in a "loop"?

AdlibEnable('_CheckFunction', 10);ever 10 milleseconds (fast enough?)
While 1
    ;Do Nothing
    Sleep(1000000)
WEnd

Func _CheckFunction()
    ;Whatever you are doing here
EndFunc
What's so hard about that?
AdlibEnable() basically keeps checking something regularly every certain amount of time. That's exactly what a loop (with sleep () inside) does.

Yeah, the we increase the frequency to make the checking fast enough. But as I said, I need it runs at the background. High frequency checking will consume too much resources. That's something I want to avoid.

That's why I am so eager for a event driven way.

Link to comment
Share on other sites

hooo so complicated

use WMI instead

i've some scripts that monitor the files, and send and email after

look in the scriptcenter on MSDN

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Link to comment
Share on other sites

  • Moderators

AdlibEnable() basically keeps checking something regularly every certain amount of time. That's exactly what a loop (with sleep () inside) does.

Yeah, the we increase the frequency to make the checking fast enough. But as I said, I need it runs at the background. High frequency checking will consume too much resources. That's something I want to avoid.

That's why I am so eager for a event driven way.

How may files are you checking? How often (give a time this time) must they be checked? Is there only going to be 1 thing going on (the checking of the files) with this script? How many languages do you know? If it's more then what you know here in AutoIt, and Loops and AdlibEnable() are out of the question, sounds like you need to be writing in another one.

@arcker: How would he use your theory without a loop or AdlibEnable in AutoIt?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

How may files are you checking? How often (give a time this time) must they be checked? Is there only going to be 1 thing going on (the checking of the files) with this script? How many languages do you know? If it's more then what you know here in AutoIt, and Loops and AdlibEnable() are out of the question, sounds like you need to be writing in another one.

@arcker: How would he use your theory without a loop or AdlibEnable in AutoIt?

no fixed time. In fact, the file will be modified by an external program. What I want is to react as fast as I can.

Languages I know? Java, C++, Javascript. But honestly, I don't know how to do an event-driven using C++.

Link to comment
Share on other sites

@Smoke_N

My scripts use GUIEventMode to continue monitoring and interactivity

without a loop it's impossible, but it's not a real loop, because it's an event loop

so wmi wait for the event, and pass to the next

i don't have reallu time to understand what does he want exactly, but wmi is a good way, and for free :/

here is an extract from my scripts :

$strComputer = inputbox("Monitor on which computer ???",'Leave "." for localhost',".")
$path=FileOpenDialog("Select the file to monitor",@ScriptDir,"all files (*.*)")
$objWMIService = Objget("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
        $strComputer & "\root\cimv2")
$path=StringReplace($path,"\","\\")         
ConsoleWrite("path => " & $path & @crlf)
$colMonitoredEvents = $objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceModificationEvent WITHIN 10 WHERE " _
        & "TargetInstance ISA 'CIM_DataFile' and " _
            & "TargetInstance.Name="""&$path&"""")
            

while 1
    $objLatestEvent = $colMonitoredEvents.NextEvent
    consolewrite ("File: " & $objLatestEvent.TargetInstance.Name & @crlf)
    ConsoleWrite( "New size: " & $objLatestEvent.TargetInstance.FileSize & @crlf)
    
    consolewrite("Old size: " & $objLatestEvent.PreviousInstance.FileSize & @CRLF)
    sleep(100)
WEnd
Edited by arcker

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Link to comment
Share on other sites

It sounds like you want to create a Copy Hook Handler to monitor changes in a folder. See:

http://windowssdk.msdn.microsoft.com/en-us...y/ms649568.aspx

I don't know if this can be done in Autoit, the only examples I've seen are in VC++ and Delphi

I have seen somebody used hook in Autoit before. But I have no idea how to do it myself. Anyway, that might be what I want

Edited by jing
Link to comment
Share on other sites

  • Moderators

I have seen somebody used hook in the Autoit before. But I have no idea how to do it myself. But it is exactly something I want

I want the new Porsche 911 Turbo, I saw someone drive it once, no idea how to obtain one.

All-wheel drive, Rear engine, 3.6 l, 6 cylinders, Torque 460 lb.-ft. @ 1,950 - 5,000 rpm, 480 hp (SAE) @ 6,000 rpm

0-60 mph: 3.9 sec.

Top Track Speed: 192.6 mph (310 km/h)

$ 122,900.00

Will someone buy it for me?

The above (however true the want is) is how I saw your post. "I saw it once" / "I need it now" / "I have no idea how to do it".

I supplied a "dream/possible reality", but no foundation on what I'm going to do to achieve it, or what I've done thus far to.

A hint to finding things that interest you.

1. Bookmarks are a wonderful thing.

2. There's a keep track option in the forum as well as a notify option

3. Remember "key words" that are used in the post, that's the only way I can find anything with the search option (either who wrote it, or something specific that stood out) and use the advanced search options.

4. Google it.

If you've done the above, then provide "source code" and or "links" to the areas that you are searching, maybe someone can provide a better search criteria, and or the actual answer you wish to obtain based on the links/code you provide.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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