5
$\begingroup$

Suppose I had about 500 .JPG and .jpg files in my folder "c:\images\". How to ask Mathematica to load all the .jpg and .JPG file names then make a case insensitive sorted list out of them, say {"butterfly.jpg", "Fish.jpg", "marilyn.jpg", "Rabbit.JPG"... }, and finally rename the files in the folder as:

enter image description here

fnameLst = FileNameTake/@ 
 FileNames[{"*.jpg", "*.JPG", "*.jpeg", "*.JPEG"}, "c:\\images", Infinity]
$\endgroup$

1 Answer 1

7
$\begingroup$

This should do the trick.

(*Import file names and generate output names*)
folder = "C:\\images\\";
fnameLst = Sort[FileNameTake /@ FileNames[{"*.jpg", "*.JPG", "*.jpeg", "*.JPEG"}, folder, 
     Infinity]];
fnameNewLst = "img_" <> IntegerString[#, 10, 3] <> ".jpg" & /@ Range[Length@fnameLst];

(*Rename files*)
RenameFile[folder <> #[[1]], folder <> #[[2]]] & /@ Transpose[{fnameLst, fnameNewLst}]
$\endgroup$
4
  • 1
    $\begingroup$ The built-in IntegerString will do what your NumberPad is doing. $\endgroup$
    – wxffles
    Sep 4, 2014 at 2:17
  • $\begingroup$ Thanks, I didn't know about IntegerString. I'll edit my answer. $\endgroup$
    – paw
    Sep 4, 2014 at 2:19
  • $\begingroup$ paw, I see room for what I feel are improvements to this (good) answer. May I edit your post or would you prefer that I post separately? $\endgroup$
    – Mr.Wizard
    Sep 24, 2014 at 6:45
  • $\begingroup$ Feel free to edit my post senpai. Every edit that improves an answer is welcome, isn't this the philosophy of SE? $\endgroup$
    – paw
    Sep 24, 2014 at 14:31

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.