Jump to content

Looking for creative suggestions: detecting a file change


Recommended Posts

I'd like to continually monitor a certain folder and subfolder and take action when a file changes (file size, date modified, etc). I tried a simple continually-running loops that did some checks, but the problem was a 100% CPU load and high memory commitment.

I was wondering if anyone had done something similar and might have some suggestions as to what direction I might want to try. Thanks.

LD

Link to comment
Share on other sites

I'd like to continually monitor a certain folder and subfolder and take action when a file changes (file size, date modified, etc). I tried a simple continually-running loops that did some checks, but the problem was a 100% CPU load and high memory commitment.

I was wondering if anyone had done something similar and might have some suggestions as to what direction I might want to try. Thanks.

LD

I have a client-side script running on my clients that monitors (among other things) certain security-related file information, and when it detects changes, it reports to my server-side script. I see no other way to do this than polling for file changes, meaning to read the file information in question and comparing it to the previous information. To save CPU time but keep information actual, I have to balance a sleep() in my polling loop. I use Sleep(100), that way my files are checked 10 times a second which is WAY more than enough in my case, and the script CPU use is 0%. But I just tried it with Sleep(10) (this checks ofcourse 100x per second) and still the CPU use is 0%. When I don't Sleep() at all, CPU use goes up to 100% just like in your case ...

Remember, your CPU is only human, flooding it with commands gets it drowned :whistle:

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

  • Moderators

You can still use your current types, ie... filegetsize/date modification differ etc... but use AdlibEnable() instead.

Global $aFiles[1] = ['PathToMyFile'], $aCurrentSize[1] = ['13390']
HotKeySet('{ESC}', '_EXITNOW')
AdlibEnable('_FileChecks', 1000);Check files every 1 second

While 1
    Sleep(1000000)
WEnd

Func _FileChecks()
    For $iCC = 0 To UBound($aFiles) - 1
        If FileGetSize($aFiles[$iCC]) <> Number($aCurrentSize[$iCC]) Then
            MsgBox(64, 'Differ', 'Values Differ for: ' & $aFiles[$iCC]);Do whatever if the file differs
            $aCurrentSize[$iCC] = FileGetSize($aFiles[$iCC]);reset new size
        EndIf
    Next
EndFunc

Func _EXITNOW()
    Exit
EndFunc

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

@all,

Why are you guys always looking to do it the difficult way ?

These are all native windows functions as far as I know.

WMI is the key to "almost" anything, also here. :whistle:

You need to use the :

__InstanceCreationEvent

__InstanceDeletionEvent

__InstanceModificationEvent

If I have a change I will post something the Example sript section.

regards

ptrex

Link to comment
Share on other sites

Ptrex,

Many thanks. WMI is an unknown beast to me, and the 4d party software I mentioned offers good, reliable functionality without me learning anything new :-)

Andy

<shrug> personally i'd rather learn something, and have 100% control over all aspects of the solution to my problem; if it were my problem that is.

Link to comment
Share on other sites

Hi ptrex... thanks for the example! It works great for file creations and deletions... but I tried using it to detect file modifications, and no luck :whistle: I don't care if a file gets deleted... but I need to know if a new file is created, and if an existing file changes (file size, timestamp at least). Thanks!

-LD

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