2

I've have some videos (.mp4) with subtitles files (.SRT) that I need to rename. These files have long names so I can only rename them manually. I've tried Python to rename them, but it fails.

I've tried to rename it with Spacemacs and I did it using Dired SPC f j to open the folder and renamed the files going line by line: Select the line, go after the number of the video and then deleting till the dot of the extension, I created a simple macro that do this and then ran the macro on all the files in that folder.

I would like to do it with a regexp, so that I can also learn about it.

The folder looks like this in Dired:

total used in directory 477255 available 38636028
drwxrwxrwx  1 User None     28672 02-23 19:04 ..
drwxrwxrwx  1 User None      8192 02-23 19:02 .
-rw-rw-rw-  1 User None  30019832 02-23 13:13 010 Introduction to Global Economics.mp4
-rw-rw-rw-  1 User None      8514 02-23 13:13 010 Introduction to Global Economics-subtitle-en.srt
-rw-rw-rw-  1 User None    263198 02-23 13:13 011 BLS-Unemployment.pdf
-rw-rw-rw-  1 User None 108247504 02-23 13:13 011 The International Monetary Fund GDP.mp4
-rw-rw-rw-  1 User None     28101 02-23 13:14 011 The International Monetary Fund GDP-subtitle-en.srt
-rw-rw-rw-  1 User None  35996597 02-23 13:14 012 Excel Exercise 1 Analyzing IMF GDP Data.mp4
-rw-rw-rw-  1 User None     11454 02-23 12:17 012 Excel Exercise 1 Analyzing IMF GDP Data-subtitle-en.srt
-rw-rw-rw-  1 User None  37911096 02-23 13:15 012 Exercise-1.xlsx
-rw-rw-rw-  1 User None 104361394 02-23 13:15 013 Excel Exercise 1 Part 1 of 3.mp4
-rw-rw-rw-  1 User None     37892 02-23 13:11 013 Excel Exercise 1 Part 1 of 3-subtitle-en.srt
-rw-rw-rw-  1 User None  77650639 02-23 13:13 014 Excel Exercise 1 Part 2 of 3.mp4
-rw-rw-rw-  1 User None     32563 02-23 13:13 014 Excel Exercise 1 Part 2 of 3-subtitle-en.srt
-rw-rw-rw-  1 User None  94068512 02-23 13:14 015 Excel Exercise 1 Part 3 of 3.mp4
-rw-rw-rw-  1 User None     34325 02-23 13:14 015 Excel Exercise 1 Part 3 of 3-subtitle-en.srt

I want to rename the file: 010 Introduction to Global Economics.mp4 to 010.mp4 and 010 Introduction to Global Economics-subtitle-en.srt to 010.srt. Also, there are some other files that should not be renamed.

Till now, I tried this \b[0-9]\{3\}\b\( w*\)+.mp4 and it selects the .mp4 files, but how can I then tell it to keep the number of the video?

Edit: All the videos and subtitle files number have always 3 digits, from 001 to 252.

1
  • 1
    Have you tried using * m and * r commands in dired to rename with regular expressions?
    – user12563
    Feb 24, 2018 at 14:37

2 Answers 2

2

Found out, by trial and error that this works: \(\b[0-9]\{3\}\b\)\( w*\)*.*\(.mp4\|.srt\) and replace it by \1\3.

2
  • 6
    You'll be interested to learn that you can edit filenames in dired buffers directly (e.g. update all files with a regular search and replace) after enabling wdired with using the normal "toggle read only" binding (C-x C-q -- but you seem to be using spacemacs, so it might be different). This is usually more convenient than using a keyboard macro.
    – phils
    Feb 24, 2018 at 2:30
  • To add to @phils comment, if you are using vanilla Emacs or alike (without massive modifications, the dired opens by default in read-only mode, to toggle that, either use C-x -C-q as @phils said, or if you don't know the keybinding, run read-only-mode in command in the minibuffer to toggle the read-only mode, then do the changes you want, and then C-x C-s to save the changes. More explanation here: stackoverflow.com/a/15882553/1613005 May 9, 2022 at 15:43
0

In spacemacs you can use the key combinations in the other comments, or you can do SPC SPC dired-toggle-read-only to enter edit mode. Once completed, you can use SPC SPC wdired-finish-edit to complete, or SPC SPC wdired-abort-changes to abort. Once in edit mode, you can used good'ol :%s/replace this/with this/gc kind of thing to rename your files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.