Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I tried to write a bat file which helped me rename files. Command to do this in that file is "ren file1 file2" which is equivalent to RenameFile[file1,file2].

renameFiles[directory_, files_, rename_] :=
 Module[{list, i = 1, newfiles},
  newfiles = ("ren " <> #) & /@ FileNames[files];
  list = Table[
    " " <> rename <> ToString[i] <> ".png", {i, 1, Length[newfiles]}];
  While[i <= Length[newfiles], 
   newfiles[[i]] = newfiles[[i]] <> list[[i]]; i++];
a = renameFiles["E:\\Download Pictures", ___, "save"]
Export["E:\\Download Pictures\rename.txt", a]

The renameFiles[] has three arguments which take the path, files in that path and name I want to change to respectively, and then convert them into a list "ren file1 file2". Afterwards, I output it as a txt file. I tried to output it as a bat file, however it displays error after I do: Export["E:\\Download Pictures\rename.bat", a]

Therefore, I attempt to do that in a different way, which renames "rename.txt" afterwards by doing: RenameFiles["E:\\Download Pictures\rename.txt","E:\\Download Pictures\rename.bat"]

So, now, I have my "rename.bat" file. I try to use SystemOpen["E:\\Download Pictures\rename.bat"] to execute it. However, it cannot work well, and I have to do it by clicking the file.

My questions are: 1. Sometimes I get the syntax: OpenWrite::noopen: "Cannot open \!\(\"E:\\\\Download Pictures\\\\nename.txt\"\). " , how can I fix this? 2. Is this the only way I can output a bat file. 3. and, how can I execute via MMA.

Thanks a lot!

share|improve this question

Not sure why you'd want to go through all the machinations of creating a batch file instead of just doing it directly with RenameFile, but... using your function, e.g.:

rn = renameFiles["c:\\users\\rasher\\documents\\", "*.xxx", "blah"];

Export["c:\\users\\rasher\\documents\\ren.bat", rn, "Text"];



Will do it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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