If you use library Dired+ then you can use command diredp-do-eval-in-marked-recursive
to do what you want.
If you use it directly it asks you for a sexp to evaluate at the beginning of each file to be acted on. In this case, the sexp you want is just a replace-string
sexp.
So this simple command does the job:
(defun bar ()
(interactive)
(diredp-do-eval-in-marked-recursive
'(replace-string "import org.apache.log4j.Logger;"
"import org.slf4j.Logger;")))
Command diredp-do-eval-in-marked-recursive
is bound by default to key sequence M-+ @ M-:
. It's also on menu-bar menu Multiple > Marked Here and Below > Apply (Map) > Eval Sexp In.
diredp-do-eval-in-marked-recursive
is an interactive Lisp function in
dired+.el
.
(diredp-do-eval-in-marked-recursive SEXP &optional IGNORE-MARKS-P DETAILS)
Evaluate SEXP
in each of the marked files, including in marked subdirs.
Like diredp-do-eval-in-marked
but act recursively on subdirs, and do
no result-logging, error-logging, or echoing.
The files included are those that are marked in the current Dired
buffer, or all files in the directory if none are marked. Marked
subdirectories are handled recursively in the same way.
With a prefix argument, ignore all marks - include all files in this
Dired buffer and all subdirs, recursively.
When called from Lisp, optional arg DETAILS
is passed to
diredp-get-files
.
[All of the Dired+ commands that act on marked files not only in the current Dired buffer but also in Dired buffers for any subdirs in that buffer that are themselves marked, and so on, recursively, are on prefix key M-+
. Those that apply a function, evaluate a sexp, or invoke a command are on prefix key M-+ @
. And M-:
is mnemonic for evaluating a sexp. (The same is true of the non-"recursive" commands - same key bindings, but without the M-+
.)]