-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor_files.lua
62 lines (53 loc) · 1.91 KB
/
monitor_files.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
local lfs = require "lfs"
-- Function to load existing file timestamps
local function load_file_timestamps(filename)
local file_timestamps = {}
local file = io.open(filename, "r")
if not file then return file_timestamps end
for line in file:lines() do
local filepath, timestamp = line:match("([^,]+),([^,]+)")
if filepath and timestamp then
file_timestamps[filepath] = timestamp
end
end
file:close()
return file_timestamps
end
-- Function to save current file timestamps
local function save_file_timestamps(filename, file_timestamps)
local file = io.open(filename, "w")
for filepath, timestamp in pairs(file_timestamps) do
file:write(filepath .. "," .. timestamp .. "\n")
end
file:close()
end
-- Main function to monitor files for changes
local function monitor_files(file_list, timestamp_file)
local file_timestamps = load_file_timestamps(timestamp_file)
local new_file_timestamps = {}
for _, filepath in ipairs(file_list) do
local attr = lfs.attributes(filepath)
if attr then
local new_timestamp = attr.modification
new_file_timestamps[filepath] = new_timestamp
if file_timestamps[filepath] and file_timestamps[filepath] ~= tostring(new_timestamp) then
print("File changed: " .. filepath)
elseif not file_timestamps[filepath] then
print("New file detected: " .. filepath)
end
else
print("Failed to read file: " .. filepath)
end
end
save_file_timestamps(timestamp_file, new_file_timestamps)
end
-- List of files to monitor (add your file paths here)
local files_to_monitor = {
"file1.txt",
"file2.txt",
"file3.txt"
}
-- File to store file timestamps
local timestamp_storage_file = "file_timestamps.txt"
-- Monitor files for changes
monitor_files(files_to_monitor, timestamp_storage_file)