0
$\begingroup$

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!

$\endgroup$
3
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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