I have a batch of files with the same format say bmp
, the file name is 0.bmp
1.bmp
... 99.bmp
, I want to change the file names, say 0.bmp
to 99.bmp
, 1.bmp
to 98.bmp
etc. can emacs do this in dired-mode
? I use emacs under Windows.

- 12,111
- 21
- 91
- 136

- 1,998
- 3
- 27
- 43
-
after searching for some questions, I find http://stackoverflow.com/a/1510716/930125 solve a similar problem – toolchainX Dec 11 '12 at 09:52
3 Answers
You can use M-x wdired-change-to-wdired-mode to make a dired buffer editable. Afterwards a simple keyboard macro with a counter, starting from the end of the dired buffer, should do the trick for you.
If you don't want to use a macro an alternative would be:
M-x replace-regexp
Replace regexp: ^[0-9]+
Replace regexp with \,(- 99 \#&)

- 55,802
- 13
- 100
- 117
-
Mmm... this will transform `0.bmp` into `1.bmp`. The OP wants to transform 0 into 99, 1 into 98, etc. Can be done similarly changing the regexp calculation, though to `(- 99 \#&)`. – Diego Sevilla Dec 11 '12 at 12:34
-
1
-
thanks! This is exactly what I want, since I know little about `emacs-lisp`. – toolchainX Dec 11 '12 at 13:56
May be a quick a dirty answer, not generic:
First: C-x C-q in dired-mode
;
Second: M-: yank and RET the snippet:
(progn
(beginning-of-buffer)
(while (re-search-forward "\\([0-9]+\\).bmp" nil t)
(replace-match
(format "%d.bmp" (- 99 (string-to-number (match-string 1))))
nil
nil)))
Third: C-c C-c to save changes, and done.

- 3,074
- 4
- 23
- 33
I'm not sure of dired-mode
, but you can execute a simple script in the *scratch*
buffer. As you're replacing existing file names with different names, I recommend you to do a first rename to all the files ant then start right with those names:
(progn
(dotimes (i 100)
(let ((file-name (concat (number-to-string i) ".bmp")))
(rename-file file-name (concat "old" file-name))))
(dotimes (i 100)
(let ((file-name-old (concat "old" (number-to-string i) ".bmp"))
(file-name-new (concat (number-to-string (- 99 i)) ".bmp")))
(rename-file file-name-old file-name-new))))
If you copy that to the *scratch*
buffer and, past the expression you push C-x C-e, that code will do it for you.

- 28,636
- 4
- 59
- 87