Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Renaming Variables Across Files

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113610] Renaming Variables Across Files
  • From: Chris Degnen <degnen at cwgsy.net>
  • Date: Thu, 4 Nov 2010 04:01:27 -0500 (EST)

I needed to rename a variable across numerous files and so put
together a short script to do this which makes use of Vim.

It works ok for replacing unique strings, although the altered
notebooks complain that they "appear to have been edited outside of
Mathematica", but that doesn't affect use.

I wondered if anyone had other suggestions for renaming or refactoring
across files.


In case anyone should find it useful, the script I use, called
"BatchS.nb" is listed below.  To set up, all the relevant files
including BatchS.nb are expected to be in directory
"sourcedirectory".  The modified versions are written to
"targetdirectory", which should exist in advance.  (Both are first-
level subdirectories off the working directory.)  Vim should also be
installed of course.

In the example "var1" is replaced by "var2".  I run the script as
separate cells, indicated by the line spacing.  Enjoy.


(* BatchS.nb *)


SetDirectory["sourcedirectory"]


allfiles=FileNames[];


files=DeleteCases[allfiles,"BatchS.nb"];
Short[files,9]


createEditScripts[file_]:=Module[{},
commands="%s/var1/var2/g
write ../targetdirectory/"<>file<>"
quit
";
Export[file<>".vim",commands,"Text"]];


Map[createEditScripts,files];


batchfile={};
createBatchFile[file_]:=Module[{},
AppendTo[batchfile,
"vim -E -s -c \"source "<>file<>".vim\" "<>file]]


Map[createBatchFile,files];


Map[Run,batchfile]


Run["del *.vim"]


Run["del *.sw? /a"]


  • Prev by Date: Exporting to PDF in full (landscape) page size?
  • Next by Date: Re: Printout Fontsize Mystery
  • Previous by thread: Exporting to PDF in full (landscape) page size?
  • Next by thread: Re: Renaming Variables Across Files