• Lua Directory Wiper
    3 replies, posted
I originally wanted to make a script that would wipe folders with "Sensitive" content in them, but I ended up just making a full directory wiper. This script deletes anything under the directory the file is placed and ran in. All the code, because it's so small [CODE] require "lfs" local curDir = lfs.currentdir() local b = [[\]] local function checkValidDir(dir) if dir == "." or dir == ".." or dir == "main.lua" then return false else return true end end local function scanDir(dir) for fl in lfs.dir(dir) do if checkValidDir(fl) == true then if lfs.attributes(dir..b..fl, "mode") == "directory" then scanDir(dir..b..fl) lfs.rmdir(dir..b..fl) else os.remove(dir..b..fl) end end end end scanDir(curDir) [/CODE] [URL="https://github.com/blindsighter314/Directory-Wiper"]Github Mirror[/URL] Thoughts?
[lua]local function checkValidDir(dir) if dir == "." or dir == ".." or dir == "main.lua" then return false else return true end end[/lua] This can be shortened to [lua]local function checkValidDir(dir) return dir ~= "." and dir ~= ".." and dir ~= "main.lua" end[/lua] [lua]if checkValidDir(fl) == true[/lua] This can be shortened to [lua]if checkValidDir(fl)[/lua]
[QUOTE=man with hat;48369771] [lua]if checkValidDir(fl) == true[/lua] This can be shortened to [lua]if checkValidDir(fl)[/lua][/QUOTE] Cool! I thought if func() then just checked if it was valid, not if it were true. Thanks! [lua] require "lfs" local curDir = lfs.currentdir() local b = [[\]] local function checkValidDir(dir) return dir ~= "." and dir ~= ".." and dir ~= "main.lua" end local function scanDir(dir) for fl in lfs.dir(dir) do if checkValidDir(fl) then if lfs.attributes(dir..b..fl, "mode") == "directory" then scanDir(dir..b..fl) lfs.rmdir(dir..b..fl) else os.remove(dir..b..fl) end end end end scanDir(curDir) [/lua]
[QUOTE=blindsighterr;48371168]Cool! I thought if func() then just checked if it was valid, not if it were true. Thanks! [lua] require "lfs" local curDir = lfs.currentdir() local b = [[\]] local function checkValidDir(dir) return dir ~= "." and dir ~= ".." and dir ~= "main.lua" end local function scanDir(dir) for fl in lfs.dir(dir) do if checkValidDir(fl) then if lfs.attributes(dir..b..fl, "mode") == "directory" then scanDir(dir..b..fl) lfs.rmdir(dir..b..fl) else os.remove(dir..b..fl) end end end end scanDir(curDir) [/lua][/QUOTE] It actually checks if it's either [B]false[/B] or [B]nil[/B]. Unless you need to explicitly know exactly what it is, you should always use it in the way I just showed you. Makes life easier. It's not limited to function calls, you can use it in, for example, if statements. [lua]local whatever = 1 if (whatever) -- whatever is not false or nil so this would be ran end[/lua]
Sorry, you need to Log In to post a reply to this thread.