Solved

How do I find and replace text strings in a series of html fies

Posted on 2011-02-15
21
1,884 Views
Last Modified: 2013-12-16
I need to find a batch file or script to change a series of text strings in a number of html Page files within a given folder, for example:

Replace: href="Template
With:       href="suspsyst
Replace: <embed type="image/svg-xml" src="
With:       <img src="
Replace: <table border="0" cellspacing="0" cellpadding="5" width="730" bgcolor="#ffffff" xmlns:fo="http://www.w3.org/1999/XSL/Format">
With:       <table border="0" cellspacing="0" cellpadding="5" width="1000" bgcolor="#ffffff" xmlns:fo="http://www.w3.org/1999/XSL/Format" height="87%">
Replace: <img src="../common/img_num/link.gif" width="15" height="15" border="0" style="vertical-align:middle;">
with:         (no text, effectively deleting the string)

Or a script that ca be run in Golive
0
Comment
Question by:thomkat2295
  • 8
  • 3
  • 2
  • +4
21 Comments
 
LVL 2

Expert Comment

by:omesie
Comment Utility
download a copy of Notepad++ and there's an option there to scan all files in entire folders and replace specific text to whatever you like.
0
 
LVL 2

Expert Comment

by:omesie
Comment Utility
Sorry, this might help. (or just do a google search)
http://notepad-plus-plus.org/
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
Download and unzip this utility:

http://www.paulslore.com/utils/chgstr.zip

Then you can change all of your HTML files (including sub-directories) like this:

CD \parentFolder
chgstr.exe  "Replace: href=\"Template"  "href=\"suspsyst"  *.html  /s
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
ooops, I left the word "Replace:" in the search string by mistake - but you get the idea.

Run chgstr.exe /?  to see the help screen.

Generally it works like this:

  chgstr.exe  "searchString"  "replaceString"  filespec  [/options]
0
 
LVL 17

Expert Comment

by:selvol
Comment Utility
I know DOS was mentioned in the keywords of your question. But I have used 12Ghosts Replace via a .bat file or command line
1,000's of time to do millions of S&R jobs.  

It is fast, and can handle very large file and a large amount of them w/o screwing up.


12Ghosts

Taken From Help file in 12ghosts:
Command line options
To repeatedly replace the same strings in the same set of files or folders call 12-Replace via command line options. This makes it easy to create a shortcut or batch file to replace several phrases at once.

Syntax:
12replace.exe "path" "search" "replace" [/replaceall] [/nosound] [/sub] [/only] [/case] [/word] [/contents]

Example: 12replace.exe "C:\My Documents\Data" "Date: 11/16/2006" "Date: 01/11/2007"

0
 

Author Comment

by:thomkat2295
Comment Utility
Hi Guys
Thanks for yor speedy responses, I will work on each as I have the time.

As I will need to change up to 20 text strings in each file in a foder containing at least 30 files.
I was hoping to do this with just one batch, script or exe file.

Currently in Golive I can do a find replace on multiple files in a folder, however I have to set up each find replace individualy some 20 times. Then I need to go through the same process for the next foder, hence the need for a batch or sript.

I hope this better explains my objective
0
 
LVL 17

Expert Comment

by:selvol
Comment Utility
With that few files.
Just USE TEXTPAD5 .
(Free Trial)
Drag all the files into Textpad5.

Click F8
Check ALL DOCUMENTS
Insert the Search and Replace strings .


Granted You will have to do this for each instance
But it will do all the files at once.


With a DOS script. I would worry. It would take abit of luck and alot of guessing.
To write a S&R script for files with so many UNKOWN variables.


Also 12ghosts is FAST and good and will recurse the folders.

Hope this helps.

Regards
0
 
LVL 33

Assisted Solution

by:knightEknight
knightEknight earned 250 total points
Comment Utility
I don't think there is a way to avoid specifying all 20 search/replace strings.  This utility will traverse the subfolders though, so you can do it all in one batch file like this:

  CD c:\parent

  chgstr.exe  "searchString1"  "replaceString1"  *.html /s
  chgstr.exe  "searchString2"  "replaceString2"  *.html /s
  chgstr.exe  "searchString3"  "replaceString3"  *.html /s
  ...
  chgstr.exe  "searchStringN"  "replaceStringN"  *.html /s
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
... and if you have more than one filespec, it will take that too:

  chgstr.exe  "searchString1"  "replaceString1"  *.html  *.aspx   *.inc   /s
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
and note that if your search string contains double-quote characters, escape them with \

   chgstr.exe  "href=\"Template"  "href=\"suspsyst"  *.html  *.aspx  /s
0
 

Author Comment

by:thomkat2295
Comment Utility
Hi knightEknight

Thanks for your advice so far. however I am having a bit of dificulty in getting it to work due to the IDtenT error. Firstly, where should the chgstr.exe file reside, what folder? Can you provided me with and example of a batch file containing the following in a text file.

  chgstr.exe  " href="Template "  "href="suspsyst"  *.html /s
  chgstr.exe  "<embed type="image/svg-xml" src="  "<img src="  *.html /s
  chgstr.exe  "<table border="0" cellspacing="0" cellpadding="5" width="730" bgcolor="#ffffff" xmlns:fo="http://www.w3.org/1999/XSL/Format">
"  " <table border="0" cellspacing="0" cellpadding="5" width="1000" bgcolor="#ffffff" xmlns:fo="http://www.w3.org/1999/XSL/Format" height="87%">"  *.html /s

When saving the text file what extention shuold it have, and how is it executed.

While I am able to acheive a find replace on multiple files I currently have to do it on a line by line basis.
Or is it that to do the work via bat or other executable is not possible.
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 250 total points
Comment Utility
(not for points, just killing sometime...)

Firstly, where should the chgstr.exe file reside, what folder?

You really have three choices here, but let's try the easiest one.  Place the chgstr.exe file in the same folder as the htm files to be changed.

Can you provided me with and example of a batch file containing the following in a text file.

Save this script to a file with a BAT extension in the same directory as the exe and the html files, like dochg.bat.  Then to run it just type dochg at a command prompt.

@echo off
chgstr.exe  " href=\"Template " " href=\"suspsyst " *.html /s
chgstr.exe  "<embed type=\"image/svg-xml\" src=" "<img src=" *.html /s
chgstr.exe  "<table border=\"0\" cellspacing=\"0\" cellpadding=\"5\" width=\"730\" bgcolor=\"#ffffff\" xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">" "<table border=\"0\" cellspacing=\"0\" cellpadding=\"5\" width=\"1000\" bgcolor=\"#ffffff\" xmlns:fo=\"http://www.w3.org/1999/XSL/Format\" height=\"87%\">" *.html /s

Open in new window

Note that you need to preface any double quotes in the search and replace strings with a backslash so chgstr doesn't use them as the delimiters for the actual search and replace strings.

~bp
0
 
LVL 33

Assisted Solution

by:knightEknight
knightEknight earned 250 total points
Comment Utility
billprew summed it up very well - the only thing I would add is something that may go without saying, but before running the script, in your command prompt you need to change your current directory to the parent directory containing your .html files, the batch file above, and chgstr.exe.  For example:

  CD  c:\inetpub\wwwroot

Note however that you probably don't want to leave the .bat file or the chgstr.exe file in that folder after the script is done, since it could be exposed.  So after the script is run successfully, move those two files to a different folder.
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
Why delete this question?
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
knightEknight, did you read my recommendation?
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
My bad, the email said it would be deleted - sorry.
0
 

Author Comment

by:thomkat2295
Comment Utility
Hi guys, I have been on leave for the past 6 weeks and have only just caught up on all the posts. As soon as I have some time I will write a scrip bases on the post by billprew on the 02/11 taking account of the added comments by knightEknight.  

On the basis of billprew's script all " marks must be preceeded by a \ . I will work on it over the weekend and hopfuly report my sucess.
0
 

Expert Comment

by:Mod_MarlEE
Comment Utility
Starting the auto-close procedure on behalf of the Question Author, to implement the recommendations from the participating Expert(s).


-----
Everyt'ing is everyt'ing,

Mod_MarlEE
Community Support Moderator
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now