--@action Find duplicate files --@version 1.0.0 --@author TheFile.Ninja --@id find-duplicates --@description Finds files with the same content in the active pane's folder and below, lists them in a window, and marks the extra copies for you to delete. --@tags cleanup, report, views --@requires none -- Nothing is deleted here: the button marks the extra copies in a flat view -- of the folder, and Delete sends them to the Recycle Bin (Ctrl+Z brings them -- back). Of each set of copies, the one with the shortest path is kept: -- "photo.jpg" rather than "photo (1).jpg" or "Copy of photo.jpg". local p = pane.active() if p:is_virtual() then ui.message("This works on a folder on a disk or a share.", "Find duplicates") return end local root = p:path() -- Sizes first, then the first 64 KB of big files, then whole checksums. local prog = ui.progress("Looking for duplicates") prog:update(-1, root) local ok, sets = pcall(hash.find_duplicates, root, { recursive = true }) prog:finish() if not ok then ui.message(tostring(sets), "Find duplicates", "error") return end if #sets == 0 then ui.message("No two files in " .. root .. " are the same.", "Find duplicates") return end local rows, extra, wasted = {}, {}, 0 for _, set in ipairs(sets) do table.sort(set.files, function(a, b) if #a ~= #b then return #a < #b end return a < b end) for i, file in ipairs(set.files) do local folder = path.dirname(file):sub(#root + 2) rows[#rows + 1] = { path.basename(file), folder ~= "" and folder or ".", set.size, i == 1 and "kept" or "extra copy", path = file } if i > 1 then extra[#extra + 1] = file end end wasted = wasted + set.size * (set.count - 1) end ui.window{ id = "duplicates", title = "Duplicates in " .. path.basename(root), width = 860, height = 640, view = { { heading = #sets .. " files have copies" }, { text = #extra .. " extra copies take " .. item.format_size(wasted) .. ". Double-click a row to show it in the pane.", muted = true, id = "note" }, { table = { columns = { "Name", "Folder", { "Size", type = "size", width = 90 }, { "Status", width = 90 } }, rows = rows, }, id = "rows" }, { row = { { button = "Mark the extra copies", id = "mark", style = "primary" }, { spacer = true }, { button = "Close", action = { close = true } }, } }, }, on_action = function(name, values, v) if name ~= "mark" then return end -- A flat view lists every file below the folder, so all the copies can be marked. if p:path() ~= root and not p:navigate_and_wait(root, 10000) then return end if not p.is_flattened then p:flatten_and_wait(60000) end p:unmark_all() p:mark(extra) p:focus() v:update("note", { text = "Marked the " .. #extra .. " extra copies: Delete sends them to the Recycle Bin, Ctrl+Z brings them back.", color = "ok" }) end, }