[Date Index]
[Thread Index]
[Author Index]
Re: Renaming Variables Across Files
*To*: mathgroup at smc.vnet.net
*Subject*: [mg113624] Re: Renaming Variables Across Files
*From*: Albert Retey <awnl at gmx-topmail.de>
*Date*: Fri, 5 Nov 2010 05:11:22 -0500 (EST)
*References*: <iatt41$ehn$1@smc.vnet.net>
Am 04.11.2010 10:07, schrieb Chris Degnen:
> 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 principle I see no problem in using vim or another textprocessing
tool (actually I think something like sed would probably make even more
sense). On the other hand, if you want to run it from Mathematica, then
why bother about external commands? The following would do the same
thing without the need of external tools. It might for very large files
be slower but starting a vim process for every file also has its costs.
With some effort you could even make that approach much more robust by
importing notebooks as notebook expressions and only work on the content
of cells...
sourcdir = "srcdir";
targetdir = "trgdir";
filelist = Select[FileNames["*",sourcedir],FileType[#]==File&]
Map[
Export[
StringReplace[#,sourcedir->targetdir],
StringReplace[Import[#,"Text"],"var1"->"var2"],
"Text"
]&,
filelist
];
Note that the above code is untested so might not work out of the box.
You will need to set sourcedir and targetdir and targetdir is assumed to
be an empty directory, so be careful you understand what it does before
running it. Writing the changed files to another directory gives you a
chance to check whether the changes are actually what you wanted them
to be. For production use you might want to extend it so that it will
also work with files in subdirectories etc...
Another thing you might want to have a look at is the Wolfram Workbench
which has a quite powerful search which can be set up to search all
files in a directory, and it can handle search (and replace) with
Mathematica patterns...
hth,
albert
Prev by Date:
**Re: Numerical solution of the heat equation on a disk**
Next by Date:
**Re: Numerical solution of the heat equation on a disk**
Previous by thread:
**Renaming Variables Across Files**
Next by thread:
**Re: suggestions for version control or backup systems?**
| |