Skip to content

Instantly share code, notes, and snippets.

@sepisoad
Last active February 7, 2019 12:41
Show Gist options
  • Save sepisoad/4f4dbf47ff3534976d6c3d41a92daa22 to your computer and use it in GitHub Desktop.
Save sepisoad/4f4dbf47ff3534976d6c3d41a92daa22 to your computer and use it in GitHub Desktop.
rename all files in a dir to lowercase or uppercase (name only / extension only / full name)
(local mfs (require "minifs"))
(local str (require "std.string"))
(if (not (= 3 (# arg)))
(do (print "not valid input arguments")
(os.exit)))
(if (not (or (= (. arg 1) "-l") (= (. arg 1) "-u")))
(do (print "first arg must be a function [-l or -u]")
(os.exit)))
(local func (if (= (. arg 1) "-l") "lower" "upper"))
(if (not (or (= (. arg 2) "-n") (= (. arg 2) "-x") (= (. arg 2) "-f")))
(do (print "first arg must be a function [-l or -u]")
(os.exit)))
(local mode (if (= (. arg 2) "-n") "name"
(= (. arg 2) "-x") "extension"
"full"))
(if (not (mfs.exists (. arg 3)))
(do
(print "destination path does not exist")
(os.exit)))
(local path (. arg 3))
(local get-file-name (mfs.files path))
(fn trans [func mode file]
(let [nme (. (str.split file "%.") 1)
ext (. (str.split file "%.") 2)
tfn (if (= func "lower") string.lower string.upper)]
(if (= "full" mode) (.. (tfn nme) "." (tfn ext))
(= "name" mode) (.. (tfn nme) "." ext)
(= "extension" mode) (.. nme "." (tfn ext)))))
(do
(var file-name (get-file-name))
(while (~= nil file-name)
(when (not (or (= file-name ".") (= file-name "..")))
(mfs.move
(.. path "/" file-name)
(.. path "/" (trans func mode file-name))))
(set file-name (get-file-name))))
@sepisoad
Copy link
Author

sepisoad commented Feb 7, 2019

the script is written in fennel script
this file has nothing to do with clojure or clojurescript.
I had to name the script as a clojure file in order to fool github to give the code a proper color scheme,

examples:

  • full name uppercase: fennel app.fnl -u -f test-dir
  • full name lowercase: fennel app.fnl -l -f test-dir
  • name only uppercase: fennel app.fnl -u -n test-dir
  • extension only lower: fennel app.fnl -l -x test-dir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment