youtuber Posted November 12, 2018 Share Posted November 12, 2018 How to detect if a txt file is added or changed in a folder that I specified? #include <Array.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <File.au3> Local $ChangingFolder = @DesktopDir & "\Changing folder" Global $sFileToCheck = _FileListToArray($ChangingFolder, "*.txt", $FLTA_FILES, True) Global $bFileChanged = False Global $sFileDateTime = _ArrayToString(FileGetTime($sFileToCheck)) AdlibRegister("_CheckFile", 250) $Form1 = GUICreate("Form1", 493, 263) $Edit1 = GUICtrlCreateEdit("" & @CRLF, 16, 24, 457, 193, BitOR($ES_MULTILINE, $ES_AUTOVSCROLL, $WS_VSCROLL)) GUISetState(@SW_SHOW, $Form1) While 1 If $bFileChanged Then GUICtrlSetData($Edit1, "File has changed" & @CRLF, 1) $sFileDateTime = _ArrayToString(FileGetTime($sFileToCheck)) $bFileChanged = False EndIf $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func _CheckFile() If $sFileDateTime <> _ArrayToString(FileGetTime($sFileToCheck)) Then $bFileChanged = True EndFunc ;==>_CheckFile Link to comment Share on other sites More sharing options...
AdamUL Posted November 12, 2018 Share Posted November 12, 2018 (edited) Have a look at the example for _WinAPI_ReadDirectoryChanges in the Help File. Adam Edited November 12, 2018 by AdamUL Link to comment Share on other sites More sharing options...
youtuber Posted November 13, 2018 Author Share Posted November 13, 2018 Program stops responding, gui does not close expandcollapse popup#include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <APIFilesConstants.au3> #include <Array.au3> #include <MsgBoxConstants.au3> #include <WinAPIError.au3> #include <WinAPIFiles.au3> #include <WinAPIMem.au3> Global $ChangingFolder = @DesktopDir & "\Changing folder" Global $pBuffer = _WinAPI_CreateBuffer(8388608) Global $aData Global $hDirectory = _WinAPI_CreateFileEx($ChangingFolder, $OPEN_EXISTING, $FILE_LIST_DIRECTORY, BitOR($FILE_SHARE_READ, $FILE_SHARE_WRITE), $FILE_FLAG_BACKUP_SEMANTICS) If @error Then _WinAPI_ShowLastError('', 1) EndIf $Form1 = GUICreate("Form1", 493, 263) $Edit1 = GUICtrlCreateEdit("" & @CRLF, 16, 24, 457, 193, BitOR($ES_MULTILINE, $ES_AUTOVSCROLL, $WS_VSCROLL)) GUISetState(@SW_SHOW, $Form1) While 1 $aData = _WinAPI_ReadDirectoryChanges($hDirectory, BitOR($FILE_NOTIFY_CHANGE_DIR_NAME, $FILE_NOTIFY_CHANGE_LAST_WRITE, $FILE_NOTIFY_CHANGE_CREATION), $pBuffer, 8388608, 1) If Not @error Then GUICtrlSetData($Edit1, $aData & @CRLF, 1) Else _WinAPI_ShowLastError('', 1) EndIf $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Link to comment Share on other sites More sharing options...
Developers Jos Posted November 13, 2018 Developers Share Posted November 13, 2018 24 minutes ago, youtuber said: Program stops responding, gui does not close Yes, so what have you done to determine where is it hanging? It is quite important to learn how to debug your own script instead of simply dumping it here hoping somebody will do it for you. Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
BrewManNH Posted November 13, 2018 Share Posted November 13, 2018 This is unnecessary, FileGetTime can already return a string. $sFileDateTime = _ArrayToString(FileGetTime($sFileToCheck)) Also, not sure what you're attempting with this line. Global $hDirectory = _WinAPI_CreateFileEx($ChangingFolder, $OPEN_EXISTING, $FILE_LIST_DIRECTORY, BitOR($FILE_SHARE_READ, $FILE_SHARE_WRITE), $FILE_FLAG_BACKUP_SEMANTICS) Where is $File_List_Directory coming from? What is its value? It's not one of the normal parameter variables for the function. If you're using the standard includes value of 1 for that variable, that's not a valid value for that parameter in CreateFileEx. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
youtuber Posted November 13, 2018 Author Share Posted November 13, 2018 (edited) @BrewManNH $File_List_Directory variable i copied it here by default https://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_ReadDirectoryChanges.htm @Jos I get this error,When I want to close Spoiler Edited November 13, 2018 by youtuber Link to comment Share on other sites More sharing options...
Developers Jos Posted November 13, 2018 Developers Share Posted November 13, 2018 (edited) I understand what you meant, just don't like to see people being unable to tell why the script is unresponsive. A few debug statements would have quickly revealed that the _WinAPI_ReadDirectoryChanges() is blocking and only returns when you make an actual change. The helpfile also mentions that: Quote The _WinAPI_ReadDirectoryChanges() function works only in synchronous mode So it looks like you need 2 scripts to make this work. Jos Edited November 13, 2018 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
BrewManNH Posted November 13, 2018 Share Posted November 13, 2018 57 minutes ago, youtuber said: $File_List_Directory variable i copied it here by default After MUCH surfing of the bowels of Microsoft, I found where the usage comes from, but it's damned hard to find. https://docs.microsoft.com/en-us/windows/desktop/FileIO/file-access-rights-constants If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
youtuber Posted November 13, 2018 Author Share Posted November 13, 2018 (edited) I've added to a for loop there's still nothing changing expandcollapse popup#include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <File.au3> #include <Array.au3> Global $bFileChanged = False AdlibRegister("_CheckFile", 250) Local $ChangingFolder = @DesktopDir & "\Changing folder\" $sFileToCheck = _FileListToArray($ChangingFolder, "*.txt", $FLTA_FILES, True) If Not @error Then For $i = 1 To $sFileToCheck[0] $sFileDateTime = _ArrayToString(FileGetTime($sFileToCheck[$i])) Next EndIf $Form1 = GUICreate("Form1", 493, 263) $Edit1 = GUICtrlCreateEdit("" & @CRLF, 16, 24, 457, 193, BitOR($ES_MULTILINE, $ES_AUTOVSCROLL, $WS_VSCROLL)) GUISetState(@SW_SHOW, $Form1) While 1 If $bFileChanged Then GUICtrlSetData($Edit1, "File has changed " & @MIN & ":" & @SEC & @CRLF, 1) $sFileDateTime = _ArrayToString(FileGetTime($sFileToCheck)) $bFileChanged = False EndIf $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func _CheckFile() If $sFileDateTime <> _ArrayToString(FileGetTime($sFileToCheck)) Then $bFileChanged = True EndFunc ;==>_CheckFile Edited November 13, 2018 by youtuber Link to comment Share on other sites More sharing options...
Earthshine Posted November 13, 2018 Share Posted November 13, 2018 sigh. you have already been given the correct answers, use the api and another script to do it the professional way. time stamping can be disabled on any given os Skeletor 1 My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
youtuber Posted November 13, 2018 Author Share Posted November 13, 2018 @Earthshine Did you mean? #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <File.au3> #include <Array.au3> Global $bFileChanged = False AdlibRegister("_CheckFile", 250) Local $ChangingFolder = @DesktopDir & "\Changing folder" ;~ $sFileToCheck = _FileListToArray($ChangingFolder, "*.txt", $FLTA_FILES, True) $sFileToCheck = FileFindFirstFile($ChangingFolder & "\*.*") If Not @error Then $sFileDateTime = _ArrayToString(FileGetTime(FileFindNextFile($sFileToCheck))) EndIf $Form1 = GUICreate("Form1", 493, 263) $Edit1 = GUICtrlCreateEdit("" & @CRLF, 16, 24, 457, 193, BitOR($ES_MULTILINE, $ES_AUTOVSCROLL, $WS_VSCROLL)) GUISetState(@SW_SHOW, $Form1) While 1 If $bFileChanged Then GUICtrlSetData($Edit1, "File has changed " & @MIN & ":" & @SEC & @CRLF, 1) $sFileDateTime = _ArrayToString(FileGetTime($sFileToCheck)) $bFileChanged = False EndIf $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func _CheckFile() If $sFileDateTime <> _ArrayToString(FileGetTime($sFileToCheck)) Then $bFileChanged = True EndFunc ;==>_CheckFile Link to comment Share on other sites More sharing options...
Earthshine Posted November 13, 2018 Share Posted November 13, 2018 no. I mean there is a good example in that reference that was given above, and the fact that you would need a second script to make it work. My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
Developers Jos Posted November 13, 2018 Developers Share Posted November 13, 2018 love to be ignored while I think the answer is in my previous post ... whatever FrancescoDiMuro, Skeletor and Earthshine 2 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
youtuber Posted November 13, 2018 Author Share Posted November 13, 2018 I really don't understand what you mean and I'm sorry Link to comment Share on other sites More sharing options...
Earthshine Posted November 13, 2018 Share Posted November 13, 2018 1. read back through this thread 2. find the reference made, and posted again by @Jos 3. work the solution. there is a good sample in that referenced link--probably what you tried to modify it looks like almost now, what don't you understand? My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
Developers Jos Posted November 13, 2018 Developers Share Posted November 13, 2018 Ah.. that's a good approach to life... when confused simply ignore it and it for sure will go away. FrancescoDiMuro, Skeletor and Earthshine 3 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Somerset Posted November 13, 2018 Share Posted November 13, 2018 4 minutes ago, Jos said: Ah.. that's a good approach to life... when confused simply ignore it and it for sure will go away. Works for me! Turns around, and vanishes. FrancescoDiMuro and Earthshine 2 Link to comment Share on other sites More sharing options...
youtuber Posted November 14, 2018 Author Share Posted November 14, 2018 There is something I don't understand, why do I only need 2 scripts in a folder that I've just specified, while there is only a small script that will let me know when all txt files are changed? Link to comment Share on other sites More sharing options...
Developers Jos Posted November 14, 2018 Developers Share Posted November 14, 2018 (edited) You need a second script when you want to have a responsive interface for your script as the script doing the _WinAPI_ReadDirectoryChanges() call will hang on that function until something is changed. To see what I mean just add a ConsoleWrite(" put some text here" & @crlf) above and below the line containing _WinAPI_ReadDirectoryChanges() and run the script from SciTE. You will see what I mean. ( and this is what I meant with learning how to debug your own scripts.) The script containing the _WinAPI_ReadDirectoryChanges() function will have to send the information about the changes to the script that has the GUI in it, but that shouldn't be too hard. Jos Edited November 14, 2018 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
youtuber Posted November 14, 2018 Author Share Posted November 14, 2018 I tried something but still failed expandcollapse popup#include "WinAPIConv.au3" #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <File.au3> #include <Array.au3> Global $ChangingFolder = @DesktopDir & "\Changing folder" Global $bFileChanged = False $Form1 = GUICreate("Form1", 493, 263) $Edit1 = GUICtrlCreateEdit("" & @CRLF, 16, 24, 457, 193, BitOR($ES_MULTILINE, $ES_AUTOVSCROLL, $WS_VSCROLL)) GUISetState(@SW_SHOW, $Form1) While 1 $bFileChanged = _WinAPI_ReadDirectoryChanges($ChangingFolder, "*.txt", 8388608, 0, 0) If $bFileChanged Then GUICtrlSetData($Edit1, "File has changed" & @CRLF, 1) $bFileChanged = False EndIf $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func _WinAPI_ReadDirectoryChanges($hDirectory, $iFilter, $pBuffer, $iLength, $bSubtree = 0) Local $aRet = DllCall('kernel32.dll', 'bool', 'ReadDirectoryChangesW', 'handle', $hDirectory, 'struct*', $pBuffer, _ 'dword', $iLength - Mod($iLength, 4), 'bool', $bSubtree, 'dword', $iFilter, 'dword*', 0, 'ptr', 0, 'ptr', 0) If @error Or Not $aRet[0] Or (Not $aRet[6]) Then Return SetError(@error + 10, @extended, 0) $pBuffer = $aRet[2] Local $aData[101][2] = [[0]] Local $tFNI, $iBuffer = 0, $iOffset = 0 Do $iBuffer += $iOffset $tFNI = DllStructCreate('dword NextEntryOffset;dword Action;dword FileNameLength;wchar FileName[' & (DllStructGetData(DllStructCreate('dword FileNameLength', $pBuffer + $iBuffer + 8), 1) / 2) & ']', $pBuffer + $iBuffer) __Inc($aData) $aData[$aData[0][0]][0] = DllStructGetData($tFNI, "FileName") $aData[$aData[0][0]][1] = DllStructGetData($tFNI, "Action") $iOffset = DllStructGetData($tFNI, "NextEntryOffset") Until Not $iOffset __Inc($aData, -1) Return $aData EndFunc ;==>_WinAPI_ReadDirectoryChanges 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