The solution of Matt is useful, but it is a bit limited in that it allows only to execute single commands without any arguments. I wondered whether it would be possible to execute macros on marked files, and came up with the following dired command that allows to replay any (saved) macro an arbitrary number of times for each selected file.
With this command, I am finally able to semi-automatically edit many files in rather non-trivial ways!
Update: the new version allows to execute arbitrary commands (including macros) and lisp expressions.
;; Inspired by M-x edit-kbd-macro and https://superuser.com/q/176627.
(defun my-dired-do-execute (keys &optional arg)
"Execute a command in all marked files.
If an error occurs, execution in other files is not affected.
(Notably, this allows to run keyboard macros until there is an error.)
At the prompt, type any bound key sequence, or `\\[execute-extended-command]'
to choose a command by its name, or `\\[eval-expression]' to enter a Lisp expression.
The prefix ARG, if given, is passed on to the chosen command.
(list (read-key-sequence (substitute-command-keys "Key sequence to execute, \
or \\[eval-expression], or \\[execute-extended-command]: "))
(let ((cmd (if (arrayp keys) (key-binding keys) keys))
(cond ((eq cmd 'execute-extended-command)
(setq cmd (read-command "Name of command to execute: "))
(if (string-equal cmd "")
(error "No command name given")))
((eq cmd 'eval-expression)
(setq exp (read--expression "Eval in selected files: "))
(setq cmd nil))
(error "Key sequence %s is not defined" (key-description keys))))
(mapc (lambda (filename)
(setq current-prefix-arg arg)
(message "Result in file %s:" filename)
(error (message "In file %s: %S" filename err)))))