Solved

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

Posted on 2011-02-15
21
1,919 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
ID: 34902831
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
ID: 34902834
Sorry, this might help. (or just do a google search)
http://notepad-plus-plus.org/
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 34903037
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 33

Expert Comment

by:knightEknight
ID: 34903042
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
ID: 34903092
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
ID: 34903296
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
ID: 34903808
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
ID: 34906748
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
ID: 34906778
... and if you have more than one filespec, it will take that too:

  chgstr.exe  "searchString1"  "replaceString1"  *.html  *.aspx   *.inc   /s
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 34907220
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
ID: 34939664
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 53

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 34939799
(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
ID: 34942908
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
ID: 35420169
Why delete this question?
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 35420244
knightEknight, did you read my recommendation?
0
 
LVL 33

Expert Comment

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

Author Comment

by:thomkat2295
ID: 35429486
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
ID: 35510943
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

832 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