random script dump
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lowercase.lua 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env lua
  2. require('os')
  3. require('lfs')
  4. function lowercase(basename, basedir)
  5. if basedir == nil then
  6. basedir = ''
  7. end
  8. local lowercased = string.lower(basename)
  9. local path_orig = basedir .. basename
  10. local path_new = basedir .. lowercased
  11. if path_orig == path_new then
  12. print(path_orig .. ' is already lowercase!')
  13. return 1
  14. else
  15. print('Renaming: ' .. path_orig .. ' -> ' .. path_new)
  16. os.rename(path_orig,path_new)
  17. end
  18. end
  19. function is_dir(name)
  20. local ftype = lfs.symlinkattributes(name, "mode")
  21. if ftype == "directory" then
  22. return true
  23. else
  24. return false
  25. end
  26. end
  27. function find_and_rename(path)
  28. for basename in lfs.dir(path) do
  29. if basename ~= "." and basename ~= ".." then
  30. local basedir = path .. '/'
  31. local filename = basedir .. basename
  32. if is_dir(filename) then
  33. find_and_rename(filename)
  34. end
  35. lowercase(basename, basedir)
  36. end
  37. end
  38. end
  39. function test_and_execute(dir)
  40. if is_dir(dir) then
  41. find_and_rename(dir)
  42. lowercase(dir)
  43. else
  44. print(dir .. ' is not a directory or does not exist!')
  45. end
  46. end
  47. local i = 1
  48. while arg[i] do
  49. test_and_execute(arg[i])
  50. i = i + 1
  51. end