# How to rename all files in a directory

I have a directory containing about 200 mp3 files. They are strangely numbered, such that during playing they are executed in a rather unpredictable order. That is how few of them are called to give a short example:

lst1 =
{"01_04_49.mp3", "01_04_50.mp3", "01_04_51.mp3", "02_01_01.mp3",
"02_01_02.mp3", "02_01_03.mp3"}


I would like to rename them using Mathematica into something like

lst2 = {"1.mp3", "2.mp3", "3.mp3", "4.mp3", "5.mp3", "6.mp3"}


in the same order as they appear in the list above.

Any idea of how to do this?

• So, lst1 is already in the desired order? Assuming that, try MapIndexed[RenameFile[#1, IntegerString[#2[[1]]] <> ".mp3"] &, lst1]. – J. M. will be back soon Aug 13 '17 at 18:07
• @ J. M.Thank you.Could you please kindly comment, how Mma will understand, in which directory to rename the files? – Alexei Boulbitch Aug 13 '17 at 18:57
• @AlexeiBoulbitch do you have the directory on hand? Just use a FileNameJoin on both parts then. Alternatively look at FileSystemMap. – b3m2a1 Aug 13 '17 at 21:09

You can use SystemDialogInput to choose the path at run time. Expanding on J.M.'s comment, you could evaluate something like this:

Module[{names, path},
names =
{"01_04_49.mp3", "01_04_50.mp3", "01_04_51.mp3",
"02_01_01.mp3", "02_01_02.mp3", "02_01_03.mp3"};
path = SystemDialogInput["Directory", WindowTitle -> "Choose a folder"];
If[path === $Canceled,$Canceled,
SetDirectory[path];
MapIndexed[RenameFile[#1, IntegerString[#2[[1]]] <> ".mp3"] &, names];
SetDirectory[]]]

• Thank you very much. – Alexei Boulbitch Aug 14 '17 at 11:53