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$ Commented Sep 4, 2014 at 2:17
  • $\begingroup$ Thanks, I didn't know about IntegerString. I'll edit my answer. $\endgroup$ Commented 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$ Commented 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$ Commented Sep 24, 2014 at 14:31

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.