Quick.FileMonitor.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. { ***************************************************************************
  2. Copyright (c) 2015-2018 Kike Pérez
  3. Unit : Quick.FileMonitor
  4. Description : Watch for single file changes
  5. Author : Kike Pérez
  6. Version : 1.1
  7. Created : 11/09/2017
  8. Modified : 07/04/2018
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.FileMonitor;
  22. interface
  23. {$i QuickLib.inc}
  24. uses
  25. Classes,
  26. Windows,
  27. SysUtils,
  28. {$IFDEF FPC}
  29. Quick.Files;
  30. {$ELSE}
  31. System.IOUtils;
  32. {$ENDIF}
  33. type
  34. TMonitorNotify = (mnNone, mnFileCreated, mnFileModified, mnFileDeleted);
  35. TMonitorWatch = set of TMonitorNotify;
  36. TFileChangeNotify = procedure(MonitorNofify : TMonitorNotify) of object;
  37. TQuickFileMonitor = class(TThread)
  38. private
  39. fFileName : string;
  40. fTickEvent : THandle;
  41. fInterval : Integer;
  42. fNotifies : TMonitorWatch;
  43. fEnabled : Boolean;
  44. fExists : Boolean;
  45. fModifedDate : TDateTime;
  46. fCurrentMonitorNotify : TMonitorNotify;
  47. fOnChangeNotify : TFileChangeNotify;
  48. procedure Execute; override;
  49. procedure SetStatus(Status : Boolean);
  50. procedure NotifyEvent;
  51. public
  52. constructor Create;
  53. destructor Destroy; override;
  54. property FileName : string read fFileName write fFileName;
  55. property Interval : Integer read fInterval write fInterval;
  56. property Notifies : TMonitorWatch read fNotifies write fNotifies;
  57. property OnFileChange : TFileChangeNotify read fOnChangeNotify write fOnChangeNotify;
  58. property Enabled : Boolean read fEnabled write SetStatus;
  59. end;
  60. implementation
  61. constructor TQuickFileMonitor.Create;
  62. begin
  63. inherited Create(True);
  64. Self.FreeOnTerminate := False;
  65. fInterval := 1000;
  66. fExists := False;
  67. fModifedDate := 0;
  68. fCurrentMonitorNotify := mnNone;
  69. fNotifies := [mnFileCreated,mnFileModified,mnFileDeleted];
  70. fTickEvent := CreateEvent(nil, True, False, nil);
  71. Self.Resume;
  72. end;
  73. destructor TQuickFileMonitor.Destroy;
  74. begin
  75. if not Terminated then Terminate;
  76. SetEvent(fTickEvent);
  77. CloseHandle(fTickEvent);
  78. inherited;
  79. end;
  80. procedure TQuickFileMonitor.Execute;
  81. var
  82. LastModifiedDate : TDateTime;
  83. begin
  84. inherited;
  85. while not Terminated do
  86. begin
  87. fCurrentMonitorNotify := mnNone;
  88. if WaitForSingleObject(fTickEvent,fInterval) = WAIT_TIMEOUT then
  89. begin
  90. if fEnabled then
  91. begin
  92. if TFile.Exists(fFileName) then
  93. begin
  94. if fExists then
  95. begin
  96. if mnFileModified in fNotifies then
  97. begin
  98. LastModifiedDate := TFile.GetLastWriteTime(fFileName);
  99. if LastModifiedDate > fModifedDate then
  100. begin
  101. fCurrentMonitorNotify := mnFileModified;
  102. fModifedDate := LastModifiedDate;
  103. end;
  104. end;
  105. end
  106. else
  107. begin
  108. if mnFileCreated in fNotifies then fCurrentMonitorNotify := mnFileCreated;
  109. LastModifiedDate := TFile.GetLastWriteTime(fFileName);
  110. fModifedDate := LastModifiedDate;
  111. end;
  112. fExists := True;
  113. end
  114. else
  115. begin
  116. //check if file deleted
  117. if mnFileDeleted in fNotifies then
  118. begin
  119. if fExists then
  120. begin
  121. fExists := False;
  122. fCurrentMonitorNotify := mnFileDeleted;
  123. end;
  124. end;
  125. end;
  126. if fCurrentMonitorNotify <> mnNone then
  127. begin
  128. if IsConsole then NotifyEvent
  129. else Synchronize(NotifyEvent);
  130. end;
  131. end;
  132. end;
  133. end;
  134. end;
  135. procedure TQuickFileMonitor.SetStatus(Status : Boolean);
  136. begin
  137. if fEnabled <> Status then
  138. begin
  139. fEnabled := Status;
  140. //gets current values
  141. if TFile.Exists(fFileName) then
  142. begin
  143. fExists := True;
  144. fModifedDate := TFile.GetLastWriteTime(fFileName);
  145. end;
  146. end;
  147. end;
  148. procedure TQuickFileMonitor.NotifyEvent;
  149. begin
  150. if Assigned(fOnChangeNotify) then fOnChangeNotify(fCurrentMonitorNotify);
  151. end;
  152. end.