Help Please! Need to do a batch find and replace on a directory full of html files

Joined
Nov 16, 2006
Messages
96
Reaction score
7
Points
8
Location
Sheffield, UK
Your Mac's Specs
MacBook 2.0Ghz Core 2 Duo
Hi

As per the title really... I need to go through a few hundred html files and get rid of a line out of the header.

I was going to sit down and do this manually, but I thought I'd rather have a tool do it for me (after all, this is what computers are for, aren't they?)...

I just need something that can work through a directory full of .htm files and do a find and replace (replacing the line with nothing).

Any free tools available?

Anything included in the OS that can do this?

Your help would be most appreciated :)

Thanks in advance!
 

cwa107


Retired Staff
Joined
Dec 20, 2006
Messages
27,042
Reaction score
812
Points
113
Location
Lake Mary, Florida
Your Mac's Specs
14" MacBook Pro M1 Pro, 16GB RAM, 1TB SSD
My suggestion would be to try Automater in your Applications directory (assuming that it works with whatever application you use to edit HTML).
 
Joined
Mar 11, 2004
Messages
1,964
Reaction score
174
Points
63
Apple has at least two pages that might be of help. This one is a step-by-step video of creating an Automator script to batch-change file names.

This page, without video, explains it, as well.

If Zoom is turned on in the Universal Access control panel/preferences, you can enlarge the video then shrink it by holding down the Control key and sliding the mouse wheel up and down (or use the keyboard).

If part of the image enlarges off the screen, drag the mouse from screen edge to screen edge to centre it. You can choose one of the three mouse-dragging actions you might prefer by clicking on the Zoom's Options button. The choices are at the bottom.
 
OP
M
Joined
Nov 16, 2006
Messages
96
Reaction score
7
Points
8
Location
Sheffield, UK
Your Mac's Specs
MacBook 2.0Ghz Core 2 Duo
I ended up getting a guy at work to write me a VB app to do this... he did it in about 15 minutes and it re-wrote over 100 files in 2 seconds :)

Shame I had to run it on a PC :(

(BTW, it wasn't renaming files, it was finding a line of code within a file, then replacing it with some different text).

Thanks anyway :)
 
Joined
Jan 4, 2006
Messages
1,385
Reaction score
146
Points
63
Location
Hamburg, Germany
Your Mac's Specs
MacBook Pro | iMac(2.1 G5) | MacBook(2.16 C2D) | MacMini (1.67 CD) | iPhone 4 | iPad (3rd Gen)
If you are familiar with the Terminal.App, you need just a single command for that ;)

1)Open Terminal.App (/Applications/Utilities/Terminal)

2)cd to the directory where you have all the html files (eg. cd ~/Sites/Mywebsite/)

3)type find -type f | xargs perl -pi -e 's/<String you want to replace>/<String you want to be replaced with>/g' and hit enter. (eg. find /Users/novicew/newsite/ -type f | xargs perl -pi -e 's/blablabla/blabla/g')

You can also define the specific file type you want the system to search as follows.
find /Users/novicew/newsite/ -type f | xargs perl -pi -e 's/blablabla/blabla/g' *.html

Possibilities are endless, you just need to find them ;)
 
Joined
Mar 30, 2004
Messages
4,744
Reaction score
381
Points
83
Location
USA
Your Mac's Specs
12" Apple PowerBook G4 (1.5GHz)
The easy way....

Open TextWrangler (It's free. Google it. If you're editing HTML, you should have it anyway.)
Choose Search > Find
Enter the text you want to search for in the top field
Enter the replacement text in the second field
Check the Multi-File Search checkbox at the bottom of the window.
Click the Other... button. A file-chooser dialog will open.
Choose the folder that all of these files are in
Click Replace All
In the next dialog box, Decide whether you'd like the modified files to be opened (so you can review and save them individually) or saved as soon as the changes are made. Then click Proceed.

And you're done.
 
OP
M
Joined
Nov 16, 2006
Messages
96
Reaction score
7
Points
8
Location
Sheffield, UK
Your Mac's Specs
MacBook 2.0Ghz Core 2 Duo
Thanks Novicew and Technologist... Both fantastic replies that answer my question.

:)
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top