AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Crazy Scripting : FolderSpy v0.96 [ Synchronous ]
Goto page Previous  1, 2, 3, 4, 5, 6  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
tic



Joined: 22 Apr 2007
Posts: 1373

PostPosted: Mon Mar 03, 2008 8:52 pm    Post subject: Reply with quote

i would like the ahk solution, but would also like to see a dll version. is it written by you? i had written a program a while ago that i discontinued as i couldnt get multiple drives to be checked.
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 6223

PostPosted: Mon Mar 03, 2008 10:46 pm    Post subject: Reply with quote

tic wrote:
is it written by you?


I got it from net and that found that it was not working in AHK.
Luckily, the source code was included and so, I investigated - fixed - recompiled it ( VC++ 6 ) and to my joy, it is working now.

As of now, the function does not accept enough parameters to make things really flexible and I am trying to work it out.

Smile
Back to top
View user's profile Send private message
Azerty



Joined: 19 Dec 2006
Posts: 72
Location: France

PostPosted: Tue Mar 04, 2008 1:20 pm    Post subject: Reply with quote

Hi

Just my 2 cents :

*) Don't count on CreateSymbolicLink : it's not the right use, it's use is meant to create "soft" or "symbolic" links (unix-like, google "man ls" : symbolic links [new to Vista] are created using "ln -s" whereas "hard" links are created using "ln" without "-s" under unix, and with "linkd" on MS OS'es)

*) "junctions" are like mount points under unix (google "man mount").

*) the best way to spy multiple dirs (to my mind) should be : call as often as needed ReadDirectoryChangesW() specifying no LPOVERLAPPED_COMPLETION_ROUTINE, but giving a different hEvent associated to each spyed dir - create them using CreateEvent()). Once finished, enter an infinite loop (or with a boolean if clean exit might be needed) which in it's beginning calls WaitForMultipleEvents() with an array containing the handles to the events created before. When given back hand, the signaled event tells which dir has changed for an appropriate processing. Finally, when over with spying, if loop is conditioned by a boolean, you just have to change it's value during this processing. You can even use a timeout to stop waiting prematurely or to have a regularly scheduled process while waiting.
=> WaitForMultipleEvents has a limit of 64 entries in it's waiting array.

*) No sample - I got no time (sorry)

Wish you all good luck Smile
Back to top
View user's profile Send private message
Guest






PostPosted: Tue Mar 04, 2008 2:46 pm    Post subject: Reply with quote

Don't push too far the analogy with Unix. And linkd is not for hardlink, it's for junction. Use fsutil for hardlink. By the way, there is no API like WaitForMultipleEvents.
Back to top
SKAN



Joined: 26 Dec 2005
Posts: 6223

PostPosted: Tue Mar 04, 2008 3:05 pm    Post subject: Reply with quote

Anonymous wrote:
By the way, there is no API like WaitForMultipleEvents.


It is obvious Azerty meant WaitForMultipleObjects Smile
Back to top
View user's profile Send private message
Azerty



Joined: 19 Dec 2006
Posts: 72
Location: France

PostPosted: Tue Mar 11, 2008 4:43 pm    Post subject: Reply with quote

SKAN wrote:
Anonymous wrote:
By the way, there is no API like WaitForMultipleEvents.


It is obvious Azerty meant WaitForMultipleObjects Smile


that's exactly it. Thanks for correction (wrote it fast Wink )
Back to top
View user's profile Send private message
caterva



Joined: 09 Apr 2008
Posts: 14

PostPosted: Wed Apr 09, 2008 10:55 pm    Post subject: Re: Crazy Scripting : FolderSpy v0.96 [ Synchronous ] Reply with quote

I'm a newbie and I'm trying to learn AutoHotKey. So forgive me if I make some trivial questions.
Code:

Loop %WatchFolder%, 1
      WatchFolder := A_LoopFileLongPath
DllCall( "shlwapi\PathAddBackslashA", UInt,&Watchfolder )

Which is the meaning of the previous three code lines?

Moreover, once launched Folderspy advises several times for each file modification. Why?
Back to top
View user's profile Send private message
tic



Joined: 22 Apr 2007
Posts: 1373

PostPosted: Wed Apr 09, 2008 11:06 pm    Post subject: Re: Crazy Scripting : FolderSpy v0.96 [ Synchronous ] Reply with quote

caterva wrote:
I'm a newbie and I'm trying to learn AutoHotKey. So forgive me if I make some trivial questions.
Code:

Loop %WatchFolder%, 1
      WatchFolder := A_LoopFileLongPath
DllCall( "shlwapi\PathAddBackslashA", UInt,&Watchfolder )

Which is the meaning of the previous three code lines?

Moreover, once launched Folderspy advises several times for each file modification. Why?


You can see what
Code:

Loop %WatchFolder%, 1
      WatchFolder := A_LoopFileLongPath


does in the docs for looping a folder

and

Code:
DllCall( "shlwapi\PathAddBackslashA", UInt,&Watchfolder )


is similar to:

Code:
If (SubStr(WatchFolder, 0) != "\")
WatchFolder .= "\"


I think.

Whens the asynchronous one coming Skan!? Very Happy Really been looking forward to it.
Back to top
View user's profile Send private message
caterva



Joined: 09 Apr 2008
Posts: 14

PostPosted: Thu Apr 10, 2008 8:34 am    Post subject: Re: Crazy Scripting : FolderSpy v0.96 [ Synchronous ] Reply with quote

Thanks for your reply. I'm having some doubts yet.
Quote:

You can see what
Code:

Loop %WatchFolder%, 1
      WatchFolder := A_LoopFileLongPath


does in the docs for looping a folder

Which is the aim of this loop? It seems to me that the variable %WatchFolder% is overwritten at every step.
Quote:

Code:

DllCall( "shlwapi\PathAddBackslashA", UInt,&Watchfolder )


is similar to:

Code:
If (SubStr(WatchFolder, 0) != "\")
WatchFolder .= "\"


Is this line inside or outside the loop? It seems to me the latter case. So, %WatchFolder% (or its address?) assumes the last value of previous loop. Sorry, I do not understand the need of the loop yet... Sad

Quote:

Whens the asynchronous one coming Skan!? Very Happy Really been looking forward to it.

Me too. His explanation of the functioning ReadDirectoryChangesW was very very useful, so I wish to ask if he can describe the difference between using it in synchronous and asymchronous mode.
Thanks to all your indications.
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2699
Location: Australia, Qld

PostPosted: Thu Apr 10, 2008 9:40 am    Post subject: Re: Crazy Scripting : FolderSpy v0.96 [ Synchronous ] Reply with quote

caterva wrote:
Which is the aim of this loop?
To execute code for each file or folder matching the input pattern. In this case it expects there to be only one matching folder; i.e. it uses the loop only to get the full path of the file (A_LoopFileLongPath.)
Quote:
It seems to me that the variable %WatchFolder% is overwritten at every step.
It is. However, the file pattern is retrieved from %WatchFolder% before the loop begins.
Quote:
... the difference between using it in synchronous and asymchronous mode.
I presume asynchronous mode would allow the script to do other things (like respond to user input) while waiting for an event, whereas synchronous mode does not.
SKAN wrote:
The problem with synchronous mode in AHK is that AHK is single threaded and ReadDirectoryChangesW can halt/freeze every other operation when it is waiting for an event in the watched folder. Halt means: you cannot start an another thread via hotkey /settimer .. the GUI ( if any ) would not respond for any messages ( including mouse ).. not even a right click on tray icon would work!

Btw, I think
Code:
DllCall( "shlwapi\PathAddBackslashA", UInt,&Watchfolder )
is not safe since there is no assurance that WatchFolder has the capacity to hold another character. The script equivalent tic mentioned is safe, since AutoHotkey will expand the variable if necessary.
Back to top
View user's profile Send private message
caterva



Joined: 09 Apr 2008
Posts: 14

PostPosted: Thu Apr 10, 2008 9:59 am    Post subject: Re: Crazy Scripting : FolderSpy v0.96 [ Synchronous ] Reply with quote

Lexikos wrote:

In this case it expects there to be only one matching folder; i.e. it uses the loop only to get the full path of the file (A_LoopFileLongPath.)

Oh, at last I think to have understood! Equivalently, I could have written
Code:
Loop %WatchFolder%, 1
{
}
      WatchFolder := A_LoopFileLongPath

Isn't it?
Many thanks to all you for your patience and kindness.

I'm very interested in an example of asynchronous mode!
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2699
Location: Australia, Qld

PostPosted: Thu Apr 10, 2008 10:11 am    Post subject: Reply with quote

No, A_LoopFileLongPath is only valid inside a loop.

I noticed "asynchronous" mode is actually achieved via an "overlapped operation." Since I have written code for such before, I thought I'd apply it to FolderSpy.

The modified ReadDirectoryChanges():
Code:
ReadDirectoryChanges() {       ;    http://msdn2.microsoft.com/en-us/library/aa365465.aspx

    Global hDir,PointerFNI, Sizeof_FNI, WatchSubdirs
   
    ; Create an event object and OVERLAPPED structure.
    hEvent := DllCall("CreateEvent", UInt, 0, Int, true, Int, false, UInt, 0)
    VarSetCapacity(Overlapped, 20, 0), NumPut(hEvent, Overlapped, 16)
   
    ; Call ReadDirectoryChangesW in overlapped/asynchronous mode.
    if !DllCall( "ReadDirectoryChangesW"
                , UInt , hDir
                , UInt , PointerFNI
                , UInt , SizeOf_FNI
                , UInt , WatchSubDirs
                , UInt , ( FILE_NOTIFY_CHANGE_FILE_NAME   := 0x1   )
                       | ( FILE_NOTIFY_CHANGE_DIR_NAME    := 0x2   )
                       | ( FILE_NOTIFY_CHANGE_ATTRIBUTES  := 0x4   )
                       | ( FILE_NOTIFY_CHANGE_SIZE        := 0x8   )
                       | ( FILE_NOTIFY_CHANGE_LAST_WRITE  := 0x10  )
                       | ( FILE_NOTIFY_CHANGE_LAST_ACCESS := 0x20  )
                       | ( FILE_NOTIFY_CHANGE_CREATION    := 0x40  )
                       | ( FILE_NOTIFY_CHANGE_SECURITY    := 0x100 )
                , UIntP, nReadLen
                , UInt , &Overlapped
                , UInt , 0  )
        return false ; error?
   
    Loop {
        ; Wait for the event to be signaled, or any window message received.
        r := DllCall("MsgWaitForMultipleObjectsEx", UInt, 1, UIntP, hEvent
                                            , UInt, -1, UInt, 0x4FF, UInt, 0x6)
        if (r = 0) || (r = -1) ; WAIT_OBJECT_0 or WAIT_FAILED
            break
        Sleep, 1 ; Allow AutoHotkey to process/dispatch messages.
    }
   
    DllCall( "CloseHandle", UInt,hEvent )

    return DllCall( "GetOverlappedResult", UInt, hDir
                    , UInt, &Overlapped, UIntP, nReadLen, Int, true )
}
...and simplified loop in WatchFolder:
Code:
  Loop {
    ReadDirectoryChanges()
    GoSub, Decode_FILE_NOTIFY_INFORMATION
    Sleep 100
    If !Watch
        Break
  }

Btw, it seems FolderSpy does not allow the user (me) to select the root of a drive. (File/folder loops don't seem to consider it a folder...)
Back to top
View user's profile Send private message
caterva



Joined: 09 Apr 2008
Posts: 14

PostPosted: Thu Apr 10, 2008 4:07 pm    Post subject: Reply with quote

Lexikos wrote:
No, A_LoopFileLongPath is only valid inside a loop.

Damn, you are right, I'm a stupid man. So, when there are not brackets, I must assume that the loop involves the following only one line?

And what about the several notifications I receive for each file change once FolderSpy is executed?
Back to top
View user's profile Send private message
tic



Joined: 22 Apr 2007
Posts: 1373

PostPosted: Thu Apr 10, 2008 4:27 pm    Post subject: Reply with quote

Lexikos is that function you posted above the asynchronous version (by this I mean we can watch several folders/drives "simultaneously"). If so, please could you show how the function should be used if more than 1 folder is to be watched. Thanks
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 6223

PostPosted: Thu Apr 10, 2008 4:40 pm    Post subject: Reply with quote

tic wrote:
please could you show how the function should be used if more than 1 folder is to be watched.


+1

I too have been successful with asynchronous method.. but I failed miserably trying to make this work for multiple folders.

so, please Lexikos.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Page 4 of 6

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group