Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Paths for the log and file list
- local logFilePath = "/disk/check_log.txt"
- local fileListPath = "/disk/file_list.txt"
- -- Function to write to the log file
- local function log(message)
- local logFile = fs.open(logFilePath, "a")
- if logFile then
- logFile.writeLine(message)
- logFile.close()
- else
- print("Error: Could not open log file.")
- end
- end
- -- Function to record a file path into the file list
- local function recordFile(filePath)
- local fileList = fs.open(fileListPath, "a")
- if fileList then
- fileList.writeLine(filePath)
- fileList.close()
- else
- print("Error: Could not open file list.")
- end
- end
- -- Function to check if a file exists and is readable
- local function checkFile(filePath)
- if not fs.exists(filePath) then
- local msg = "Error: File does not exist " .. filePath
- print(msg)
- log(msg)
- os.sleep(1) -- Delay for 1 second
- return false
- end
- local file = fs.open(filePath, "r")
- if not file then
- local msg = "Error: Could not open file " .. filePath
- print(msg)
- log(msg)
- os.sleep(1) -- Delay for 1 second
- return false
- end
- local success, errorMessage = pcall(function()
- local content = file.readAll()
- file.close()
- end)
- if not success then
- local msg = "Error reading file " .. filePath .. ": " .. errorMessage
- print(msg)
- log(msg)
- os.sleep(1) -- Delay for 1 second
- return false
- end
- local msg = "File " .. filePath .. " is okay."
- print(msg)
- log(msg)
- os.sleep(1) -- Delay for 1 second
- return true
- end
- -- Function to recursively check files in a directory, excluding /disk/users subdirectories
- local function checkDirectory(directory)
- local files = fs.list(directory)
- for _, file in ipairs(files) do
- local filePath = directory .. "/" .. file
- if fs.isDir(filePath) then
- -- Skip /disk/users subdirectories
- if not filePath:match("^/disk/users") then
- checkDirectory(filePath)
- end
- else
- recordFile(filePath) -- Record the file in the file list
- checkFile(filePath)
- end
- end
- end
- -- Function to check files from the recorded file list
- local function checkRecordedFiles()
- if fs.exists(fileListPath) then
- local fileList = fs.open(fileListPath, "r")
- if fileList then
- for line in fileList.readLine do
- checkFile(line)
- os.sleep(1) -- Delay for 1 second
- end
- fileList.close()
- else
- print("Error: Could not open file list for reading.")
- end
- else
- print("No previous file list found. Scanning all files...")
- os.sleep(2) -- Longer delay before starting full scan
- checkDirectory("/disk")
- end
- end
- -- Main program
- print("Starting file check...")
- log("Starting file check...")
- -- Clear previous log file if it exists
- if fs.exists(logFilePath) then
- fs.delete(logFilePath)
- end
- -- Clear previous file list if it exists
- if fs.exists(fileListPath) then
- fs.delete(fileListPath)
- end
- -- Start checking files
- checkDirectory("/disk")
- print("File check complete.")
- log("File check complete.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement