Jump to content

About detecting changes to files?


youtuber
 Share

Recommended Posts

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

Program stops responding, gui does not close

#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

  • Developers
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

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 Gude
How 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

  • Developers

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

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 Gude
How 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

I've added to a for loop there's still nothing changing :(

#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 by youtuber
Link to comment
Share on other sites

@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

  • Developers

 love to be ignored while I think the answer is in my previous post ...   whatever

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

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

  • Developers

Ah.. that's a good approach to life...  when confused simply ignore it and it for sure will go away. ;) 

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

  • Developers

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

I tried something but still failed

#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

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