Quick.FileMonitor.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. { ***************************************************************************
  2. Copyright (c) 2015-2017 Kike Pérez
  3. Unit : Quick.FileMonitor
  4. Description : Watch for single file changes
  5. Author : Kike Pérez
  6. Version : 1.0
  7. Created : 11/09/2017
  8. Modified : 16/09/2017
  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. uses
  24. Classes,
  25. Windows,
  26. System.SysUtils,
  27. System.IOUtils;
  28. type
  29. TMonitorNotify = (mnNone, mnFileCreated, mnFileModified, mnFileDeleted);
  30. TMonitorWatch = set of TMonitorNotify;
  31. TFileChangeNotify = procedure(MonitorNofify : TMonitorNotify) of object;
  32. TQuickFileMonitor = class(TThread)
  33. private
  34. fFileName : string;
  35. fTickEvent : THandle;
  36. fInterval : Integer;
  37. fNotifies : TMonitorWatch;
  38. fEnabled : Boolean;
  39. fExists : Boolean;
  40. fModifedDate : TDateTime;
  41. fCurrentMonitorNotify : TMonitorNotify;
  42. fOnChangeNotify : TFileChangeNotify;
  43. procedure Execute; override;
  44. procedure SetStatus(Status : Boolean);
  45. procedure NotifyEvent;
  46. public
  47. constructor Create;
  48. destructor Destroy; override;
  49. property FileName : string read fFileName write fFileName;
  50. property Interval : Integer read fInterval write fInterval;
  51. property Notifies : TMonitorWatch read fNotifies write fNotifies;
  52. property OnFileChange : TFileChangeNotify read fOnChangeNotify write fOnChangeNotify;
  53. property Enabled : Boolean read fEnabled write SetStatus;
  54. end;
  55. implementation
  56. constructor TQuickFileMonitor.Create;
  57. begin
  58. inherited Create(True);
  59. Self.FreeOnTerminate := False;
  60. fInterval := 1000;
  61. fExists := False;
  62. fModifedDate := 0;
  63. fCurrentMonitorNotify := mnNone;
  64. fNotifies := [mnFileCreated,mnFileModified,mnFileDeleted];
  65. fTickEvent := CreateEvent(nil, True, False, nil);
  66. Self.Resume;
  67. end;
  68. destructor TQuickFileMonitor.Destroy;
  69. begin
  70. if not Terminated then Terminate;
  71. SetEvent(fTickEvent);
  72. CloseHandle(fTickEvent);
  73. inherited;
  74. end;
  75. procedure TQuickFileMonitor.Execute;
  76. var
  77. LastModifiedDate : TDateTime;
  78. begin
  79. inherited;
  80. while not Terminated do
  81. begin
  82. fCurrentMonitorNotify := mnNone;
  83. if WaitForSingleObject(fTickEvent,fInterval) = WAIT_TIMEOUT then
  84. begin
  85. if fEnabled then
  86. begin
  87. if TFile.Exists(fFileName) then
  88. begin
  89. if fExists then
  90. begin
  91. if mnFileModified in fNotifies then
  92. begin
  93. LastModifiedDate := TFile.GetLastWriteTime(fFileName);
  94. if LastModifiedDate > fModifedDate then
  95. begin
  96. fCurrentMonitorNotify := mnFileModified;
  97. fModifedDate := LastModifiedDate;
  98. end;
  99. end;
  100. end
  101. else
  102. begin
  103. if mnFileCreated in fNotifies then fCurrentMonitorNotify := mnFileCreated;
  104. LastModifiedDate := TFile.GetLastWriteTime(fFileName);
  105. fModifedDate := LastModifiedDate;
  106. end;
  107. fExists := True;
  108. end
  109. else
  110. begin
  111. //check if file deleted
  112. if mnFileDeleted in fNotifies then
  113. begin
  114. if fExists then
  115. begin
  116. fExists := False;
  117. fCurrentMonitorNotify := mnFileDeleted;
  118. end;
  119. end;
  120. end;
  121. if fCurrentMonitorNotify <> mnNone then
  122. begin
  123. if IsConsole then NotifyEvent
  124. else Synchronize(NotifyEvent);
  125. end;
  126. end;
  127. end;
  128. end;
  129. end;
  130. procedure TQuickFileMonitor.SetStatus(Status : Boolean);
  131. begin
  132. if fEnabled <> Status then
  133. begin
  134. fEnabled := Status;
  135. //gets current values
  136. if TFile.Exists(fFileName) then
  137. begin
  138. fExists := True;
  139. fModifedDate := TFile.GetLastWriteTime(fFileName);
  140. end;
  141. end;
  142. end;
  143. procedure TQuickFileMonitor.NotifyEvent;
  144. begin
  145. if Assigned(fOnChangeNotify) then fOnChangeNotify(fCurrentMonitorNotify);
  146. end;
  147. end.