Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to check if a file contains a specific word or number
- local function fileContains(filePath, pattern)
- local file = fs.open(filePath, "r")
- if file then
- local content = file.readAll()
- file.close()
- if string.find(content, pattern) then
- return true
- end
- end
- return false
- end
- -- Function to recursively search through directories for files containing the pattern
- local function searchDirectory(directoryPath, pattern, filesToDelete)
- for _, item in ipairs(fs.list(directoryPath)) do
- local itemPath = directoryPath .. "/" .. item
- if fs.isDir(itemPath) then
- searchDirectory(itemPath, pattern, filesToDelete)
- else
- if item ~= "System" and fileContains(itemPath, pattern) then
- table.insert(filesToDelete, itemPath)
- end
- end
- end
- end
- -- Main program
- local function main()
- local searchPattern = "yourWordOrNumber" -- Change this to the word or number you are searching for
- local directoriesToSearch = {"/disk", "/recovery"}
- local filesToDelete = {}
- -- Iterate through the specified directories
- for _, directory in ipairs(directoriesToSearch) do
- if fs.exists(directory) and fs.isDir(directory) then
- searchDirectory(directory, searchPattern, filesToDelete)
- end
- end
- -- If no files found, inform the user and exit
- if #filesToDelete == 0 then
- print("No files containing the pattern were found.")
- return
- end
- -- Show the list of files to delete
- print("The following files contain the pattern and will be deleted:")
- for _, file in ipairs(filesToDelete) do
- print(file)
- end
- -- Ask for confirmation
- print("Do you want to delete these files? (yes/no)")
- local response = read()
- if response == "yes" then
- -- Delete the files
- for _, file in ipairs(filesToDelete) do
- fs.delete(file)
- end
- print("Files deleted.")
- else
- print("Deletion aborted.")
- end
- end
- -- Run the main program
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement