Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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},
  SetDirectory[directory];
  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++];
  Return[newfiles]
  ]
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

1 Answer 1

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"];

SetDirectory["c:\\users\\rasher\\documents\\"];

Run["ren.bat"]

Will do it.

share|improve this answer

Your Answer

 
discard

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.