Flaghacker

File renamer

Jul 31st, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.05 KB | None | 0 0
  1. fs.rename = fs.move
  2.  
  3. local function digits(str)
  4.   local a, b
  5.   local c, d
  6.   for i = 0, #str do
  7.     c, d = a, b
  8.     a, b = str:find(string.rep("%d", i))
  9.     if not a then
  10.       break
  11.     end
  12.   end
  13.   return c, d
  14. end
  15.  
  16. local function split(str)
  17.   local a, b = digits(str)
  18.   local prefix = str:sub(1, a - 1)
  19.   local digits = tonumber(str:sub(a, b))
  20.   local zerocount = (b - a + 1) - #tostring(digits)
  21.   local suffix = str:sub(b + 1, #str)
  22.   return prefix, digits, zerocount, suffix
  23. end
  24.  
  25. local function add(str, val)
  26.   local data = {split(str)}
  27.   data[2] = tostring(data[2] + val)
  28.   local name = data[1]..string.rep("0", data[3])..data[2]..data[4]
  29.   return name
  30. end
  31.  
  32. function renameFiles(val, loc)
  33.   local loc = loc or "/rename"
  34.   if not fs.exists(loc) then
  35.     error("Location folder ("..loc..") doesn't exists", 2)
  36.   for _, oldName in pairs(fs.list(loc)) do
  37.     if not fs.isDir(oldName) then
  38.       local newName = add(name, val)
  39.       fs.rename(oldName, newName)
  40.       print("Renamed "..oldName.." to "..newName..".")
  41.     end
  42.   end
  43. end
Advertisement
Add Comment
Please, Sign In to add comment