Created
November 23, 2019 21:11
-
-
Save Elmuti/515ef514c8533064a6886616e3569c45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local lfs = require("lfs") | |
local files = {} | |
local nf = 0 | |
local sz = 0 | |
local io, os, error = io, os, error | |
local fs = {} | |
local run = true | |
local CREATE_DUMMY_FILES = false --CREATE DUMMY FILES FOR DEBUG REASONS | |
local types = { | |
["Images"] = {"png", "jpg", "jpeg", "gif", "svg", "ico", "tiff", "pdn", "xcf", "tga", "dds", "bmp", "ai", "ps", "eps"}; | |
["Audio Files"] = {"aif", "iff", "m3u", "mp3", "m4a", "sfk", "mpa", "ra", "wav", "wma", "ogg", "flac"}; | |
["Executables"] = {"exe", "jar", "vb", "vbs", "bat", "lua", "com", "apk", "app", "gadget", "pif"}; | |
["Text Documents"] = {"doc", "docx", "log", "msg", "odt", "pages", "rtf", "tex", "wpd", "wps"}; | |
["Video Files"] = {"mp4", "3gp", "asf", "asx", "mov", "mpg", "srt", "swf", "wmv"}; | |
["Image Files"] = {"iso", "bin", "cue", "dmg", "mdf", "toast", "vcd"}; | |
["System Files"] = {"dll", "sys", "dmp", "cpl", "cab", "drv", "sys"}; | |
["Data Files"] = {"csv", "dat", "key", "pps", "ppt", "pptx"}; | |
["3D Files"] = {"obj", "3ds", "max", "3dm", "bsp", "vmf"}; | |
["Text Files"] = {"txt", "cpp", "c", "ini", "cfg", "xml"}; | |
["Archives"] = {"rar", "zip", "7z", "gzip", "tar.gz", "tar", "gz"}; | |
["Spreadsheets"] = {"xlr", "xls", "xlsx"}; | |
["Torrents"] = {"torrent"}; | |
["Installers"] = {"msi"}; | |
} | |
function Round(num, idp) | |
local mult = 10^(idp or 0) | |
return math.floor(num * mult + 0.5) / mult | |
end | |
function fs.exists(path) | |
local file = io.open(path, 'rb') | |
if file then | |
file:close() | |
end | |
return file ~= nil | |
end | |
function fs.read(path, mode) | |
mode = mode or '*a' | |
local file, err = io.open(path, 'rb') | |
if err then | |
error(err) | |
end | |
local content = file:read(mode) | |
file:close() | |
return content | |
end | |
function fs.write(path, content, mode) | |
mode = mode or 'w' | |
local file, err = io.open(path, mode) | |
if err then | |
error(err) | |
end | |
file:write(content) | |
file:close() | |
end | |
function fs.copy(src, dest) | |
local content = fs.read(src) | |
fs.write(dest, content) | |
end | |
function fs.move(src, dest) | |
os.rename(src, dest) | |
end | |
function fs.remove(path) | |
os.remove(path) | |
end | |
function FormatSize(size) | |
if size >= 1000^4 then | |
return (size / 1000^4).."Tb" | |
elseif size >= 1000^3 then | |
return (size / 1000^3).."Gb" | |
elseif size >= 1000^2 then | |
return (size / 1000^2).."Mb" | |
elseif size >= 1000 then | |
return (size / 1000).."Kb" | |
end | |
return size.."bytes" | |
end | |
function IterateFiles(path) | |
for file in lfs.dir(path) do | |
if file ~= "." and file ~= ".." then | |
local f = path.."/"..file | |
local attr = lfs.attributes(f) | |
assert(type(attr) == "table", "Unexpected error, file attributes could not be read") | |
if attr.mode ~= "directory" then | |
nf = nf + 1 | |
sz = sz + attr.size | |
local ft = GetFileType(file) | |
lfs.mkdir(path.."/"..FolderFromType(ft)) | |
fs.move(path.."/"..file, path.."/"..FolderFromType(ft).."/"..file) | |
end | |
end | |
end | |
end | |
function FolderFromType(t) | |
for folderName, ftypes in pairs(types) do | |
for n, ftype in pairs(ftypes) do | |
if ftype == t then | |
return folderName | |
end | |
end | |
end | |
return "Miscellaneous" | |
end | |
function GetFileType(f) | |
local d = string.find(f, "%.") | |
if d then | |
return f:sub(d + 1, f:len()) | |
end | |
return nil | |
end | |
local dummyfiles = 0 | |
function main() | |
os.execute("cls") | |
-- ####### DEBUG START ####### -- | |
if CREATE_DUMMY_FILES then | |
for folderName, ftypes in pairs(types) do | |
for n, ftype in pairs(ftypes) do | |
dummyfiles = dummyfiles + 1 | |
local file = io.open("DLS/FILE."..ftype, "w") | |
local str = string.rep( | |
tostring(math.random()).."\n", | |
64 | |
) | |
file:write(str) | |
file:close() | |
end | |
end | |
os.execute("cls") | |
io.write(dummyfiles.." dummy files created\n\nPress ENTER to continue") | |
io.read() | |
os.execute("cls") | |
end | |
-- ####### DEBUG END ####### -- | |
os.execute("color 70") | |
os.execute("title LSortByFileType") | |
io.write("This program will take all the files in the chosen folder and\nput them in their own respective folders inside the chosen\nfolder. Sorting is done based on file type.\n\n\n\nNOTE: SORTING CANNOT BE REVERSED!\n\n\nType folder path:\n") | |
local path = io.read() | |
if path == "debug" then | |
CREATE_DUMMY_FILES = not CREATE_DUMMY_FILES | |
return | |
end | |
os.execute("cls") | |
io.write("Sorting files, Please wait...") | |
local now = os.clock() | |
IterateFiles(path) | |
if nf <= 0 then | |
os.execute("cls") | |
io.write("0 files were found at that path, are you sure the path was entered right?") | |
io.read() | |
return | |
else | |
local dur = os.clock() - now | |
os.execute("cls") | |
io.write("Sort complete!\n"..nf.." files("..FormatSize(sz)..") were sorted in "..Round(dur, 4).." seconds.\n\nPress ENTER to exit") | |
io.read() | |
run = false | |
end | |
end | |
while run do | |
local s, e = pcall(main) | |
if not s then | |
print(e) | |
io.read() | |
break | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment