Link to home
Start Free TrialLog in
Avatar of Antonio King
Antonio King

asked on

Add text to htm doc via Batch file.

I would like to know how to make a batch script that could open and edit numerous htm files.

We have a folder on our network that contains everyone's email signatures in .htm format.
I would like to know how to create a batch script that will open each one of these files (without me having to specify the filenames), find a particular line of text and add or change the text.

IE: Our signature has the collegues number, then below fax number.
We would like to now add our signature after that...
So the batch would have to find XXXXX XXXXXX (fax number) then add <br>www.xxxxxxx.com after it.

Any advice on this would be greatly appreciated!

Many thanks,
ASKER CERTIFIED SOLUTION
Avatar of basicinstinct
basicinstinct
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of F. Dominicus
you have left a few details
Are the file_names the xxxxxxx in the <br> www.xxxxxxx ?
How doe soyu colledgues number really look?
How does the HTML format look?

My assumptions are
the name of the file is what should be placed in the xxxx
I assume also that the fax number is preceeded with a Fax. and that
there are exactly 5 numbers followed by one whitspace followed by 6 numbers.
However this can be change easily.

Now you talked about a batch file. I don't know if you meant it literally that you want to write such stuff
in the bath language of Windows. So I used Ruby for solving your requirements.
The solutoin looks like this:
#!/usr/bin/ruby

Dir["*.html"].each { | file |
  fh = File.open(file)
  f_mod = File.open( "#{file}.new", "w");
  while line = fh.gets
    line.chomp!
    if line =~ /Fax: \d{5,5} \d{6,6}/
      f_mod.puts("#{line} <br>www.#{file}.com")
    else
      f_mod.puts(line)
    end
  end
  fh.close
  f_mod.close
}

I made  a new file in which I saved the results. If you are happy with the outcoming you simply
could use something like File.rename for renaming the new file name to the old ones.

Regards
Friedrich
Avatar of Antonio King
Antonio King

ASKER

Hi guys, this is an example of the html code in our signatures, they're all the same, the only things changed are the names, roles and telephone/fax numbers.

<html>
<head>
<title>CBSbutler Email Signature</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>&nbsp;</p>
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="299" rowspan="3"><img src="http://www.cbsbutler.com/emailimages/signature_cbsbutler_logo.gif"></td>
    <td width="271" colspan="2"><div align="right"><font color="004782" size="2" face="Arial, Helvetica, sans-serif"><strong>Collegues Name</strong></font></div></td>
  </tr>
  <tr>
    <td colspan="2"><div align="right"><font color="004782" size="2" face="Arial, Helvetica, sans-serif">collegues Role</font></div></td>
  </tr>
  <tr>
    <td width="121"><div align="right"><font color="004782" size="2" face="Arial, Helvetica, sans-serif"><strong>Tel:<br>
    Fax:</strong></font></div></td>
    <td width="150"><div align="right"><font color="004782" size="2" face="Arial, Helvetica, sans-serif">+XX (X)XXXX XXXXXX<br>
      +XX (X)XXXX XXXXXX
    </font></div></td>
  </tr>
</table>
<table width="570" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="right" height="5"></td>
  </tr>
  <tr>
    <td align="right"><img src="http://www.cbsbutler.com/emailimages/signature_orangestrip.jpg" height="1" width="200"></td>
  </tr>
  <tr>
    <td align="right" height="5"></td>
  </tr>
</table>
<table width="570" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="506"><img src="http://www.cbsbutler.com/emailimages/signature_awardstext.gif"></td>
    <td width="64"><img src="http://www.cbsbutler.com/emailimages/signature_award.JPG" width="64" height="92"></td>
  </tr>
</table>
<table width="570" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="5"></td>
  </tr>
  <tr>
    <td><img src="http://www.cbsbutler.com/emailimages/signature_bluestrip.jpg"></td>
  </tr>
</table>
</body>
</html>

As there are over 50 signatures I thought it would be easier to have a program or script that could find certain parts in the file and amend or add to.

The parts I'm trying to ADD TO are

<strong>Tel:<br>
    Fax:</strong></font></div></td>
To
<strong>Tel:<br>
    Fax:<br>Web:</strong></font></div></td>

and...

+XX (X)XXXX XXXXXX<br>
      +XX (X)XXXX XXXXXX
To
+XX (X)XXXX XXXXXX<br>
      +XX (X)XXXX XXXXXX<br>www.cbsbutler.com

I suggested a batch file as I thought the script would need to be made as a batch file. Any other methods will be sufficient.

Cheers for your input so far people, basicinstinct's method did not work, and i am unsure as to what "Ruby" is or stands for.

Thanks!

Ah yes the joys of generated html.
First remark: Ruby is a programming language http://www.ruby-lang.org/en/
And not the trouble strikes. the pattern will get ugly and it's not clear from where the cbsbutler may come.
let us assume we can take the first entry from <title> and lower that name and get the middle part of the web address
and let us assume also that latter
+XX (X)XXXX XXXXXX is on it's own line, then this modification will do the job:

#!/usr/bin/ruby

Dir["*.html"].each { | file |
  fh = File.open(file)
  f_mod = File.open( "#{file}.new", "w");
  name = ""
  while line = fh.gets
    line.chomp!
    if line =~ /<title>(\w+) /
      name = $1.dup # save away the name
      name.downcase! # make it into lower case letters
      f_mod.puts(line) # don't do anything with the line
    elsif line =~ /\s+\+\d{2,2} \(\d\)\d{4,4}/ #omitted the last 6 numbers
      f_mod.puts("#{line} <br>www.#{name}.com")
    else
      f_mod.puts(line)
    end
  end
  fh.close
  f_mod.close
}


However the number probabylly won't be that strict but there's not other line in the .file which start with a + and not other character but blanks in front of it, so it could be safe just to match the the + on it's own line.

Regards
Friedrich
might not do what you wanted - does exactly what you asked for
@BasicInstinct...

Fiddled around with your code, and got it working perfect to how I want.

Thanks!
oh cool, good stuff, cheers Alan