Frage

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.

War es hilfreich?

Lösung

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 \#&)

Andere Tipps

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.

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top