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 

folder monitor
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
alexizle



Joined: 28 Aug 2009
Posts: 7

PostPosted: Fri Aug 28, 2009 9:51 am    Post subject: folder monitor Reply with quote

i wrote this script to monitor a folder of mine wich is public to the network that people kept addig too without telling me

it will pop up with a dialog box for user selection of which folder to monitor
and then if that folders size decreases/increases a message box will how up to notify you.

however it was having a few bugs so lets call this the beta version

suggestions on improvements welcome



Code:
#Persistent
FileSelectFolder, OutputVar , , 1, select folder to monitor

Loop, %OutputVar%\*.*, , 1
    FolderSize += %A_LoopFileSize%
DoThis:
Folder2Size = 0
Loop, %OutputVar%\*.*, , 1
    Folder2Size += %A_LoopFileSize%
if (Folder2Size != FolderSize)
{
   MsgBox, folder has been modified
   FolderSize = 0
   Loop, %OutputVar%\*.*, , 1
    FolderSize += %A_LoopFileSize%
}

SetTimer , DoThis, 1000000
return
Back to top
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Fri Aug 28, 2009 10:57 am    Post subject: Reply with quote

Check out WatchDirectory()
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
ZANZA



Joined: 10 Aug 2009
Posts: 12

PostPosted: Mon Aug 31, 2009 10:17 am    Post subject: Reply with quote

works great for me!

suggestion store the file size in the reg\txt file. cause if you restart the computer it will reset the file size again.

Other then that nice job!
_________________
Beta. Software undergoes beta testing shortly before it's released. Beta is Latin for "still doesn't work."

The programmer's national anthem is 'AAAAAAAARRRRGHHHHH!!'.
Back to top
View user's profile Send private message Visit poster's website
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Mon Aug 31, 2009 2:10 pm    Post subject: Reply with quote

This is not reliable due to the timer. Folder can be changed many times in between.
_________________
Back to top
View user's profile Send private message
ZANZA



Joined: 10 Aug 2009
Posts: 12

PostPosted: Mon Aug 31, 2009 11:47 pm    Post subject: Reply with quote

majkinetor wrote:
This is not reliable due to the timer. Folder can be changed many times in between.


I just changed the timer down to 1 be it doesn't really matter
_________________
Beta. Software undergoes beta testing shortly before it's released. Beta is Latin for "still doesn't work."

The programmer's national anthem is 'AAAAAAAARRRRGHHHHH!!'.
Back to top
View user's profile Send private message Visit poster's website
purloinedheart



Joined: 04 Apr 2008
Posts: 537
Location: Canada

PostPosted: Mon Aug 31, 2009 11:52 pm    Post subject: Reply with quote

What if someone goes in and writes all of the files backwards?
Back to top
View user's profile Send private message
ZANZA



Joined: 10 Aug 2009
Posts: 12

PostPosted: Tue Sep 01, 2009 1:57 am    Post subject: Reply with quote

PurloinedHeart wrote:
What if someone goes in and writes all of the files backwards?


what do you mean? copy the files back into the folder?
_________________
Beta. Software undergoes beta testing shortly before it's released. Beta is Latin for "still doesn't work."

The programmer's national anthem is 'AAAAAAAARRRRGHHHHH!!'.
Back to top
View user's profile Send private message Visit poster's website
purloinedheart



Joined: 04 Apr 2008
Posts: 537
Location: Canada

PostPosted: Tue Sep 01, 2009 10:40 pm    Post subject: Reply with quote

If you open a file in the folder, select some text, cut, and paste somewhere else and save, it won't be detected because the file size will be the same.
Back to top
View user's profile Send private message
alexizle



Joined: 28 Aug 2009
Posts: 7

PostPosted: Thu Sep 03, 2009 9:23 am    Post subject: Reply with quote

suggestions on how to fix that?
Back to top
View user's profile Send private message
alexizle



Joined: 28 Aug 2009
Posts: 7

PostPosted: Thu Sep 03, 2009 9:26 am    Post subject: Reply with quote

Quote:
If you open a file in the folder, select some text, cut, and paste somewhere else and save, it won't be detected because the file size will be the same.


this code is made only to let me know when my folks send me stuff across the network without telling me and then 2 weeks later say 'havnt you seen that yet?' so your problem doesnt really apply. however i would like to make this code more universal for later use so if anyone has suggestions on how to fix that then let me know.
Back to top
View user's profile Send private message
alexizle



Joined: 28 Aug 2009
Posts: 7

PostPosted: Thu Sep 03, 2009 9:28 am    Post subject: Reply with quote

also the timer is only so long because i was testing it and wanted to space out the message. just change the timer down to 1 and you shouldnt have a problem.
Back to top
View user's profile Send private message
purloinedheart



Joined: 04 Apr 2008
Posts: 537
Location: Canada

PostPosted: Thu Sep 03, 2009 3:11 pm    Post subject: Reply with quote

Maybe you could use A_LoopFileTimeAccessed to keep a sum of all the files? I think it would work better than checking size
Back to top
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Thu Sep 03, 2009 4:08 pm    Post subject: Reply with quote

Can you explain why you do not use WatchDirectory for that?

What is wrong with this example Question
Code:
#Persistent
OnExit, GuiClose

WatchFolders=C:\Temp*|%A_Temp%*|%A_Desktop%|%A_DesktopCommon%|%A_MyDocuments%*|%A_ScriptDir%|%A_WinDir%*

Gui,Add,ListView,r10 w600 vWatchingDirectoriesList gShowDir,WatchingDirectories|WatchingSubdirs
Loop,Parse,WatchFolders,|
   WatchDirectory(SubStr(A_LoopField,0)="*" ? (SubStr(A_LoopField,1,StrLen(A_LoopField)-1)) : A_LoopField
               , SubStr(A_LoopField,0)="*" ? 1 : "")
               ,LV_Add("",SubStr(A_LoopField,0)="*" ? (SubStr(A_LoopField,1,StrLen(A_LoopField)-1)) : A_LoopField
               , SubStr(A_LoopField,0)="*" ? 1 : 0)
LV_ModifyCol(1,"AutoHdr")
Gui,Add,ListView,r30 w600 vChangesList gShowFile,Action|Dir|FileName  -  Double click to open in explorer
Gui,Add,Button,gAdd Default,Watch new directory
Gui,Add,Button,gDelete x+1,Stop watching all directories
Gui,Add,Button,gClear x+1,Clear List
Gui,Add,StatusBar,,Changes Registered
Gui, Show

SetTimer, WatchFolder, -100
Return

Clear:
Gui,ListView, ChangesList
LV_Delete()
Return

Delete:
WatchDirectory()
Gui,ListView, WatchingDirectoriesList
LV_Delete()
Gui,ListView, ChangesList
TotalChanges:=0
SB_SetText("Changes Registered")
Return

ShowDir:
If A_GuiEvent!=DoubleClick
   Return
Gui,ListView,%A_GuiControl%
LV_GetText(folder,A_EventInfo,1)
Run,% "explorer.exe /e`, /n`, /select`," . folder
Return
ShowFile:
If A_GuiEvent!=DoubleClick
   Return
Gui,ListView,%A_GuiControl%
LV_GetText(folder,A_EventInfo,2)
LV_GetText(file,A_EventInfo,3)
Run,% "explorer.exe /e`, /n`, /select`," . (folder . "\" . file)
Return

Add:
Gui,+OwnDialogs
FileSelectFolder,dir,,3,Select directory to watch for
SetTimer,SetMsgBoxButtons,-50
MsgBox, 262146,Add directory,Would you like to watch for changes in:`n%dir%
SplashTextOn,600,0,Adding %dir% ...
Gui,ListView, WatchingDirectoriesList
IfMsgBox Retry
   WatchDirectory(dir),LV_Add("",dir,1)
IfMsgBox Ignore
   WatchDirectory(dir,0),LV_Add("",dir,0)
SplashTextOff
LV_ModifyCol(1,"AutoHdr")
Gui,ListView, ChangesList
Return

SetMsgBoxButtons:
   ControlSetText,Button2,&Incl. subdirs, ahk_class #32770
   ControlSetText,Button3,&Excl. subdirs, ahk_class #32770
Return

WatchFolder:
   WatchDirectory("ReportDirectoryChanges")
   SetTimer, WatchFolder, -10
Return

ReportDirectoryChanges(Action,folder,file){
   static
   #1:="New File", #2:="Deleted", #3:="Modified", #4:="Renamed From", #5:="Renamed To"
   ;ToolTip % #%Action% "`n" folder . (SubStr(folder,0)="\" ? "" : "\") . file
   path:=folder . (SubStr(folder,0)="\" ? "" : "\") . file
   Gui,ListView, ChangesList
   SplitPath,path,filename,dir
   LV_Add("",#%Action%,dir,filename)
   LV_ModifyCol()
   LV_ModifyCol(3,"AutoHdr")
   TotalChanges++
   SB_SetText("Changes Registered " . TotalChanges)
}

GuiClose:
WatchDirectory()
ExitApp




;Function WatchDirectory()
;
;Parameters
;      WatchFolder         - Specify a valid path to watch for changes in.
;                     - can be directory or drive (e.g. c:\ or c:\Temp)
;                     - can be network path e.g. \\192.168.2.101\Shared)
;                     - can include last backslash. e.g. C:\Temp\ (will be reported same form)
;
;      WatchSubDirs      - Specify whether to search in subfolders
;
;StopWatching   -   THIS SHOULD BE DONE BEFORE EXITING SCRIPT AT LEAST (OnExit)
;      Call WatchDirectory() without parameters to stop watching all directories
;
;ReportChanges
;      Call WatchDirectory("ReportingFunctionName") to process registered changes.
;      Syntax of ReportingFunctionName(Action,Folder,File)
;
;      Example
/*
      #Persistent
      OnExit,Exit
      WatchDirectory("C:\Windows",1)
      SetTimer,WatchFolder,100
      Return
      WatchFolder:
         WatchDirectory("RegisterChanges")
      Return
      RegisterChanges(action,folder,file){
         static
         #1:="New File", #2:="Deleted", #3:="Modified", #4:="Renamed From", #5:="Renamed To"
         ToolTip % #%Action% "`n" folder . (SubStr(folder,0)="\" ? "" : "\") . file
      }   
      Exit:
         WatchDirectory()
      ExitApp
*/

WatchDirectory(WatchFolder="", WatchSubDirs=true)
{
   static
   local hDir, hEvent, r, Action, FileNameLen, pFileName, Restart, CurrentFolder, PointerFNI, _SizeOf_FNI_=65536
   nReadLen := 0
   If !(WatchFolder){
      Gosub, StopWatchingDirectories
   } else if IsFunc(WatchFolder) {
      r := DllCall("MsgWaitForMultipleObjectsEx", UInt, DirIdx, UInt, &DirEvents, UInt, -1, UInt, 0x4FF, UInt, 0x6) ;Timeout=-1
      if !(r >= 0 && r < DirIdx)
         Return
      r += 1
      CurrentFolder := Dir%r%Path
      PointerFNI := &Dir%r%FNI
      DllCall( "GetOverlappedResult", UInt, hDir, UInt, &Dir%r%Overlapped, UIntP, nReadLen, Int, true )
      Loop {
         pNext      := NumGet( PointerFNI + 0  )
         Action      := NumGet( PointerFNI + 4  )
         FileNameLen := NumGet( PointerFNI + 8  )
         pFileName :=       ( PointerFNI + 12 )
         If (Action < 0x6){
            VarSetCapacity( FileNameANSI, FileNameLen )
            DllCall( "WideCharToMultiByte",UInt,0,UInt,0,UInt,pFileName,UInt,FileNameLen,Str,FileNameANSI,UInt,FileNameLen,UInt,0,UInt,0)
            %WatchFolder%(Action,CurrentFolder,SubStr( FileNameANSI, 1, FileNameLen/2 ))
         }
         If (!pNext or pNext = 4129024)
            Break
         Else
            PointerFNI := (PointerFNI + pNext)
      }
      DllCall( "ResetEvent", UInt,NumGet( Dir%r%Overlapped, 16 ) )
      Gosub, ReadDirectoryChanges
      return r
   } else {
      Loop % (DirIdx) {
         If InStr(WatchFolder, Dir%A_Index%Path){
            If (Dir%A_Index%Subdirs)
               Return
         } else if InStr(Dir%A_Index%Path, WatchFolder) {
            If (WatchSubDirs){
               DllCall( "CloseHandle", UInt,Dir%A_Index% )
               DllCall( "CloseHandle", UInt,NumGet(Dir%A_Index%Overlapped, 16) )
               Restart := DirIdx, DirIdx := A_Index
            }
         }
      }
      If !Restart
         DirIdx += 1
      r:=DirIdx
      hDir := DllCall( "CreateFile"
                , Str  , WatchFolder
                , UInt , ( FILE_LIST_DIRECTORY := 0x1 )
                , UInt , ( FILE_SHARE_READ     := 0x1 )
                     | ( FILE_SHARE_WRITE    := 0x2 )
                     | ( FILE_SHARE_DELETE   := 0x4 )
                , UInt , 0
                , UInt , ( OPEN_EXISTING := 0x3 )
                , UInt , ( FILE_FLAG_BACKUP_SEMANTICS := 0x2000000  )
                     | ( FILE_FLAG_OVERLAPPED       := 0x40000000 )
                , UInt , 0 )
      Dir%r%         := hDir
      Dir%r%Path     := WatchFolder
      Dir%r%Subdirs  := WatchSubDirs
      VarSetCapacity( Dir%r%FNI, _SizeOf_FNI_ )
      VarSetCapacity( Dir%r%Overlapped, 20, 0 )
      DllCall( "CloseHandle", UInt,hEvent )
      hEvent := DllCall( "CreateEvent", UInt,0, Int,true, Int,false, UInt,0 )
      NumPut( hEvent, Dir%r%Overlapped, 16 )
      if ( VarSetCapacity(DirEvents) < DirIdx*4 and VarSetCapacity(DirEvents, DirIdx*4 + 60))
         Loop %DirIdx%
         {
            If (SubStr(Dir%A_Index%Path,1,1)!="-"){
               action++
               NumPut( NumGet( Dir%action%Overlapped, 16 ), DirEvents, action*4-4 )
            }
         }
      NumPut( hEvent, DirEvents, DirIdx*4-4)
      Gosub, ReadDirectoryChanges
      If Restart
         DirIdx = %Restart%
   }
   Return
   StopWatchingDirectories:
      Loop % (DirIdx) {
         DllCall( "CloseHandle", UInt,Dir%A_Index% )
         DllCall( "CloseHandle", UInt,NumGet(Dir%A_Index%Overlapped, 16) )
         Dir%A_Index%=
         Dir%A_Index%Path=
         Dir%A_Index%Subdirs=
         Dir%A_Index%FNI=
         DllCall( "CloseHandle", UInt, NumGet(Dir%A_Index%Overlapped,16) )
         VarSetCapacity(Dir%A_Index%Overlapped,0)
      }
      DirIdx=
      VarSetCapacity(DirEvents,0)
   Return
   ReadDirectoryChanges:
      DllCall( "ReadDirectoryChangesW"
         , UInt , Dir%r%
         , UInt , &Dir%r%FNI
         , UInt , _SizeOf_FNI_
         , UInt , Dir%r%SubDirs
         , 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_CREATION    := 0x40  )
               | ( FILE_NOTIFY_CHANGE_SECURITY    := 0x100 )
         , UInt , 0
         , UInt , &Dir%r%Overlapped
         , UInt , 0  )
   Return
}

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
ZANZA



Joined: 10 Aug 2009
Posts: 12

PostPosted: Fri Sep 04, 2009 2:30 am    Post subject: Reply with quote

HotKeyIt wrote:
Can you explain why you do not use WatchDirectory for that?

What is wrong with this example Question


umm 20 lines compared to ....
_________________
Beta. Software undergoes beta testing shortly before it's released. Beta is Latin for "still doesn't work."

The programmer's national anthem is 'AAAAAAAARRRRGHHHHH!!'.
Back to top
View user's profile Send private message Visit poster's website
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Fri Sep 04, 2009 5:05 am    Post subject: Reply with quote

ZANZA wrote:
HotKeyIt wrote:
Can you explain why you do not use WatchDirectory for that?

What is wrong with this example Question


umm 20 lines compared to ....


So it's too complex Question
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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