Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Editing multiple files

Posted by enquirer (enquirer), 16 December 2003
I have a problem, simple, but, I'm a newbie, hence, finding it difficult:

I would like to change text, within multiple files.
replace text "audio/audio.html" inside a file

with ...

directory name/filename.html


when a file is found in xx/yy.html (file called yy.html, in the directory
called xx)
then replace text within it, from
"audio/audio.html" to "xx/yy.html"

If you can help, I'd be very grateful.

Posted by graham (graham), 16 December 2003
A surprisingly common requirement in Perl - to go through all the files in a directory and replace all occurrences of one string in all files with a particular extension with another string.    Here's a sample program that does it:


print "Directory path? ";
chop ($dp = <STDIN>);

print "File extension? ";
chop ($fe = <STDIN>);

print "Replace: ";
chop ($togo = <STDIN>);

print "By: ";
chop ($tobecome = <STDIN>);

opendir (DH,$dp) or die "Can't access directory\n";

while ($olditem = $item = readdir DH) {
       if ($item !~ /\.$fe$/) {
               print "Skipping $item\n";
       $olditem =~ s/\.$fe$/.bak/;
       rename "$dp/$item","$dp/$olditem";
       open (FHI,"$dp/$olditem");
       open (FHO,">$dp/$item");
       read(FHI,$buffer,-s "$dp/$olditem");
       $count = ($buffer =~ s/$togo/$tobecome/g);
       print (($count+0)." changes to file $item\n");
       print FHO $buffer;

and the result from a sample run ....

[Graham-Elliss-Computer:~/dec03] graham% perl multi_edit
Directory path? demo
File extension? html
Replace: and
By: &amp;
Skipping .
Skipping ..
Skipping bits.php4
Skipping ctown.php4
Skipping demopic.jpg
Skipping index.php4
Skipping page.bak
1 changes to file page.html
Skipping secdo.php4
Skipping sh.php4
Skipping sheep.jpg
Skipping T243.bak
56 changes to file T243.html
Skipping tab.bak
0 changes to file tab.html
Skipping upload.php4
Skipping WHC.css
[Graham-Elliss-Computer:~/dec03] graham%

Note that this example
a) does not decend into subdirectories
b) Uses full perl regular expressions for the string to be replaced
c) Takes a backup copy of each file being potentially changed
d) Gives a verbose report which could easily be shortened
e) Is designed to be fast on lots of small files; won't work on huge files that are too big to be loaded into memory in a single read

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho