Copying multiple files with different filenames (with Emacs)

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: abc.txt, def.txt and ghi.txt

(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.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: