--@action Tidy a folder as files arrive --@version 1.0.0 --@author TheFile.Ninja --@id tidy-as-they-arrive --@description Watches a folder (your Downloads, say) and moves each new file into a subfolder for its kind: Pictures, Documents, Archives… Put it in Scripts\Autostart\. --@tags cleanup, downloads, watcher --@requires file_write -- The first time it runs it asks which folder to watch (the active pane's is -- offered) and remembers it. The moves go through the app's file operations: -- the strip under the panes shows them, a clash is asked there, and Ctrl+Z -- puts a file back (it is then left where it is). -- Which subfolder each kind of file goes to. Change them as you like; a file -- that matches none stays where it is. local KINDS = { Pictures = { "jpg", "jpeg", "png", "gif", "webp", "heic", "svg", "bmp" }, Documents = { "pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "odt", "ods", "txt", "md", "csv" }, Archives = { "zip", "7z", "rar", "gz", "tar" }, Installers = { "exe", "msi", "msix" }, Video = { "mp4", "mkv", "mov", "webm", "avi" }, Music = { "mp3", "flac", "m4a", "wav", "ogg" }, } -- Files a browser is still downloading: left alone until they get their name. local UNFINISHED = { crdownload = true, part = true, partial = true, download = true, tmp = true } local folder = script.data_get("folder") if not folder or not fs.is_dir(folder) then folder = ui.input("Which folder should be tidied as files arrive?", "Tidy a folder", pane.active():path()) if not folder or folder == "" or not fs.is_dir(folder) then return end script.data_set("folder", folder) end local kind_of = {} for sub, exts in pairs(KINDS) do for _, ext in ipairs(exts) do kind_of[ext] = sub end end local moved = {} -- what was moved once is left alone when it comes back (Ctrl+Z) local function tidy(file) local sub = kind_of[path.ext(file):lower()] if not sub or UNFINISHED[path.ext(file):lower()] or moved[file:lower()] or not fs.is_file(file) then return end local dest = path.join(folder, sub) if not fs.is_dir(dest) then local ok, err = fs.create_dir(dest) if not ok then ui.log_error("Tidy: could not make " .. dest .. ": " .. tostring(err)) return end end moved[file:lower()] = true file_ops.move(file, dest) ui.status(path.basename(file) .. " → " .. sub) end local w = watcher.create(folder, { recursive = false, debounce_ms = 1500 }) w:on("created", function(e) tidy(e.path) end) w:on("renamed", function(e) tidy(e.path) end) -- a browser's download that is done print("Tidying " .. folder .. " as files arrive")