Edit multiple files at once using change.py

For many years I have used a home grown tool that allows me to make simple changes to multiple files with one command. It is called change and has existed as csh, bash and perl incarnations over the years. I recently rewrote it in python and thought that it might be useful to others so I am publishing it. Here is an example of how it works:

#!/bin/bash
files=$(find . -type f)
change.py 'Copyright (c) 2010' 'Copyright (c) 2012' $files
change.py 'Version 1.0' 'Version 1.2' $files

I suspect that lots of you have written your own, similar tools. If that is the case, please look it over send me suggestions for improvements.

You can download it here: http://projects.joelinoff.com/change-1.0/change.py.

Make sure that you call it using the python command or edit the first line to correctly access your local version of python.

Have fun!

Here is the on-line help (-h or –help). It explains things in a bit more detail.

% python ./change.py -h

USAGE
change.py [OPTIONS]

DESCRIPTION
This a tool that allows you to conveniently change text in a group
of files using regular expressions. It will also change file names.

You specify the old pattern, the new pattern and the list of files.

The patterns are in python regular expression format which makes it
easy to change text that contains things like forward slashes.

Here is a detailed example thats shows how to change foo to bar.

% ls -1
foo.txt
foo-bar.txt
% cat foo.txt
foo foo
% cat foo-bar.txt
foo foo
% change.py -s foo bar foo.txt foo-bar.txt
Total Files Analyzed : 2
Files Changed : 2
Files With Content Changes : 2
Files With Name Changes : 2
Total Changes : 4
Elapsed Time : 1.00 msecs
Time Per File : 0.50 msecs
% ls -1
bar.txt
bar-bar.txt

If you want to substitute characters that used in regular
expressions like '.' you must escape it with a backslash '\.'.
Here is an example:

% # Change all 3 character strings that start with 'fo'.
% change.py 'fo.' 'bar' *

% # Only change strings that have 'fo' and a period: 'fo.'.
% change.py 'fo\.' 'bar' *

If you only want to change a work use the '\b' directive as
follows:

% # Change 'foo' to 'bar' but leave 'foobar' alone.
% change.py '\bfoo\b' 'bar' *

OPTIONS
-C, --keep-contents
Don't change file contents. By default file contents
change.

-d, --dir DIR
Output directory. By default the files are changed
in place. If this option is specified, then changed
files appear in <dir>/<xfn> where xfn is the file
name with the unique portion of directory for all
file names in the list prepended. This reduces the
depth of the output direction. This is useful for
debugging.

-h, --help Display this usage message.

-n, --noop No change. It only tells you what would be changed.

-N, --keep-names
Don't change file names. By default file names change.

-s, --stats Print out run stats.

-v, --verbose
Increase the level of verbosity.
This can be specified multiple times.

-V, --version
Print the program version and exit.

EXAMPLES
% # Help
% change.py -h

% # Change foo to bar for the C++ files in this directory.
% change.py -s 'foo' 'bar' *.cc *.h

% # Change old to new for all Python files in this directory
% # tree.
% change.py -s 'old' 'new' `find . -type f -name '*.py`

% # Change old to new for all Python files but leave the
% # file names unchanged.
% change.py -s -N 'old' 'new' `find . -type f -name '*.py`

AUTHOR
Joe Linoff

VERSION
Version 1.0

LICENSE
This for this change tool.

Copyright (c) 2011 by Joe Linoff

The change tool is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

The change tool is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details. You should have received
a copy of the GNU General Public License along with the change
tool; if not, write to the Free Software Foundation, Inc., 59
Temple Place, Suite 330, Boston, MA 02111-1307 USA.

2 thoughts on “Edit multiple files at once using change.py”

  1. Help ! Maybe ?
    I scanned a bunch of pictures into different directories (by year). The file name in each directory came up as “scan001.jpg, scan002.jpg etc. I now realize if I want to move files from 1 directory to another, I will have to rename every one.
    I don’t know what “python” is. I am running on Vista 2006 Home”.
    Do you know a way to change just the first 4 chacters of the file name (to the year) but retain the number portion of the name. (example: scan001 would become 1963001, 0r scan001 in a diferent directory would become 1985001.
    any assist is greatly appreciated !!
    THANKS,

    1. > I don’t know what “python” is.

      Python is a scripting language like C#, perl or ruby. It is free and open-source (like perl and ruby) so it won’t cost you anything to download and use.

      The change tool can definitely do what you want. You simply have to run it as follows:

      > cd
      > python change.py –keep-contents ‘scan’ ‘1963’ *.jpg

      Unfortunately, to make it work you have to download and install python on your windows machine.

      If you want to do that navigate to this page: http://www.python.org/download/releases/2.7.2/ and select the Windows MSI (MicroSoft Installer) installer. Once it is installed, you can run change.py from a DOS window as shown above.

      If you do not wish to use python, you may want to look at learning PowerShell programming or look for a windows tool that will do what you want. I am not that familiar with those sorts of tools since I do most everything from the command line.

      Good luck.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.