Situation: you have to make multiple copies of a file, but with a precise and different name each time.
And to be sure you don’t try to make your smart ass by right clicking, copying, pasting and renaming, let’s say you have a hundred of them. How to do it? Well Emacs of course! In fact my example is so constrained, that I will even add that the list of precise names is already in Emacs! (It’s actually what happened to me…)
(defun copy-file-multiple-output (original-file awesome-filename-list &optional destination-path) (let ((path (or destination-path (file-name-directory original-file))) (extension (file-name-extension original-file))) (dolist (current-name awesome-filename-list) (copy-file (concat original-file) (concat path current-name "." extension)))))
That’s it! 5 lines of code to save tens of minutes of work, which was boring and prone to human error!
The following line will take my
test.txt file and create 3 copies of it, named:
(copy-file-multiple-output "/home/frozenlock/test.txt" '("abc" "def" "ghi"))
This could probably have been done in Powershell, Bash or any other scripting tool, but… well… I live in Emacs!
Sometimes I wonder how normal people can get things done.