adg Posted January 25, 2021 Share Posted January 25, 2021 Hi, i'm tring to do a script that detects the changes in a directory. I can not use WinAPI_ReadDirectoryChanges or similar because the script will not run all the time. I was planing to read the directory tree and save it to a text file and every time the script starts it will read the file and compare with actual tree. But i'm not sure how to do that...a little help would be apreciated. Thanks a lot! expandcollapse popup#include <File.au3> #include <Array.au3> #include <String.au3> Local $sDirectoryPath = "Y:\gestor3" Local $sTree = "" Local $sFileName = "" Local $hFileOpen = FileOpen(@ScriptDir & "\gestor4.txt", $FO_READ) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.") Return False EndIf Local $sFileRead = FileRead($hFileOpen) FileClose($hFileOpen) ;HERE IT SHOULD TEST IF $sFileRead IS DIFFERENT TO $sTree AND ENUMERATE CHANGES $sTree = _CreateDirTree($sDirectoryPath,"\", "|", "-") $sFileName = @ScriptDir & "\gestor4.txt" FileDelete($sFileName) FileWrite($sFileName, $sTree) ConsoleWrite($sTree & @CRLF) ;Danyfirex - 18/06/2018 Thanks Danyfirex! Func _CreateDirTree($sDirectoryPath, $sFolderBar = "\", $sTreeBar = "|", $sSeparator = "-", $iNSeparator = 3) Local $aFiles = _FileListToArrayRec($sDirectoryPath, "*", $FLTAR_FILESFOLDERS, $FLTAR_RECUR, $FLTAR_SORT) Local $sFilePath = "" Local $sAttrib = "" Local $sFolderName = "" Local $sIndentation = "" Local $sRelativePath = "" Local $sFinalTree = "" If $aFiles[0] Then Local $sNode = $aFiles[1] For $i = 1 To $aFiles[0] $sRelativePath = $aFiles[$i] $sFilePath = $sDirectoryPath & "\" & $sRelativePath $sAttrib = FileGetAttrib($sFilePath) If StringInStr($sAttrib, "D") Then $sFolderName = _GetFolderName($sFilePath) $sIndentation = _GenerateIndentNodes($sDirectoryPath, $sRelativePath, $sFolderBar, $sTreeBar, $sSeparator) ;~ ConsoleWrite($sIndentation & $sFolderName & @CRLF) $sFinalTree &= $sIndentation & $sFolderName & @CRLF Else $sFolderName = _GetFileName($sFilePath) $sIndentation = _GenerateIndentNodes($sDirectoryPath, $sRelativePath, $sFolderBar, $sTreeBar, $sSeparator) ;~ ConsoleWrite($sIndentation & $sFolderName & @CRLF) $sFinalTree &= $sIndentation & $sFolderName & @CRLF EndIf Next EndIf Return $sFinalTree EndFunc ;==>_CreateDirTree Func _CheckFolderMore2Files($sDirectoryPath) Local $iRet = 0 Local $hSearch = FileFindFirstFile($sDirectoryPath & "\" & "*.*") If $hSearch = -1 Then Return $iRet EndIf Local $sFileName = "" Local $iCount = 0 While 1 $sFileName = FileFindNextFile($hSearch) If @error Then ExitLoop EndIf $iCount += 1 If $iCount > 1 Then $iRet = $iCount ExitLoop EndIf WEnd FileClose($hSearch) Return $iCount EndFunc ;==>_CheckFolderMore2Files Func _GenerateIndentNodes($sDirectoryPath, $sFolderRelativePath, $sFolderBar, $sTreeBar, $sSeparator, $iNSeparator = 3) Local $aSplit = StringSplit($sFolderRelativePath, "\") If $aSplit[0] > 1 Then Local $sBars = "" Local $iFiles = "" Local $sFilePath = "" For $i = 1 To $aSplit[0] - 1 $sFilePath = _ArrayToString($aSplit, "\", 1, $i) $iFiles = _CheckFolderMore2Files($sDirectoryPath & "\" & $sFilePath) If @error Then ContinueLoop EndIf If $iFiles > 1 Or $aSplit[0] - 1 = $i Then $sBars &= ($i = $aSplit[0] - 1) ? $sFolderBar & _StringRepeat($sSeparator, $iNSeparator) : $sTreeBar & _StringRepeat(" ", $iNSeparator) Else $sBars &= " " & _StringRepeat(" ", $iNSeparator) EndIf Next Return $sBars EndIf Return "" EndFunc ;==>_GenerateIndentNodes Func _GetFileName($sFullPath) Local $aSplit = StringSplit($sFullPath, "\") If $aSplit[0] Then Return $aSplit[$aSplit[0]] EndIf EndFunc ;==>_GetFileName Func _GetFolderName($sFullPath) Local $aSplit = StringSplit($sFullPath, "\") If $aSplit[0] Then Return $aSplit[$aSplit[0]] EndIf EndFunc ;==>_GetFolderName Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted January 25, 2021 Moderators Share Posted January 25, 2021 Moved to the appropriate forum, as the Developer General Discussion forum very clearly states: Quote General development and scripting discussions. Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums. Moderation Team adg 1 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
MrCreatoR Posted January 26, 2021 Share Posted January 26, 2021 expandcollapse popup#include <Array.au3> #include <File.au3> Global $sDir = @ScriptDir & '\MyMonitorDir' Global $sDirList_File = @ScriptDir & '\DirMon.txt' Global $aOld_List = (FileExists($sDirList_File) ? StringSplit(StringStripCR(FileRead($sDirList_File)), @LF) : 0) $aCompare = _DirMonitor($sDir, $aOld_List, True) Switch @error Case 0 If $aCompare[0] Then MsgBox(64, 'Added List', $aCompare[0]) EndIf If $aCompare[1] Then MsgBox(64, 'Removed List', $aCompare[1]) EndIf Case 1 MsgBox(48, @ScriptName, 'Monitor dir not found') Exit Case -1 MsgBox(64, @ScriptName, 'Nothing changed') EndSwitch $hFile = FileOpen($sDirList_File, 2) For $i = 1 To UBound($aOld_List) - 1 FileWrite($hFile, $aOld_List[$i] & ($i = $aOld_List[0] ? '' : @CRLF)) Next FileClose($hFile) Func _DirMonitor($sDir, ByRef $aOld_List, $fRecurse = False) If Not FileExists($sDir) Then Return SetError(1, 0, 0) EndIf Local $aList = _FileListToArrayRec($sDir, '*', $FLTAR_FILESFOLDERS, ($fRecurse ? $FLTAR_RECUR : $FLTAR_NORECUR), $FLTAR_NOSORT, $FLTAR_FULLPATH) If UBound($aOld_List) = 0 Then $aOld_List = $aList Return SetError(2, 0, 0) EndIf If Not IsArray($aList) Then Return SetError(3, 0, 0) EndIf Local $iFind, $sAdded = '', $sRemoved = '' For $i = 1 To $aList[0] $iFind = _ArraySearch($aOld_List, $aList[$i], 1) If $iFind = -1 Then $sAdded &= ($sAdded ? '|' : '') & $aList[$i] EndIf Next For $i = 1 To $aOld_List[0] $iFind = _ArraySearch($aList, $aOld_List[$i], 1) If $iFind = -1 Then $sRemoved &= ($sRemoved ? '|' : '') & $aOld_List[$i] EndIf Next $aOld_List = $aList If ($sAdded & $sRemoved) = '' Then Return SetError(-1, 0, 0) EndIf Local $aRet[2] = [$sAdded, $sRemoved] Return $aRet EndFunc Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now