I’m a newbie to programming, so please bear with me here…
I have a directory full of files called “foo01.txt”, “foo02.txt”, etc. and a function called MyFunction. I want to open each file as a buffer, run MyFunction on it, write the buffer to its file, kill the buffer and move on to the next file in the series until all the files are done.
I think all the pieces I need to do this are described in the Cookbook (http://emacswiki.org/emacs/ElispCookbook) but I’m not really understanding how to put it all together. Thanks!
Answer
If you’re looking for an answer in pure elisp, you could do something like this:
process-fileswill iterate over each file in a given directory and applyprocess-fileto all .txt files. You can call it like so:You can copy this into a *scratch* buffer and play around with the individual parts. The most interesting functions are:
mapc– applies a function to each item in a listdirectory-files– gets all files and folders in a directory, in this case retrieving all .txt filesfind-file– opens a file in a buffer (this is what is run when you typeC-x C-f)Learning Lisp
If you’re learning Lisp for its own sake, I can recommend Practical Common Lisp. You’ll be able to work through a surprising amount of the book using Elisp. Otherwise, download a Common Lisp environment like SBCL.