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?

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

1 Answer 1


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,
  MapIndexed[RenameFile[#1, IntegerString[#2[[1]]] <> ".mp3"] &, names];
  • $\begingroup$ Thank you very much. $\endgroup$ Aug 14, 2017 at 11:53

Your Answer

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

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