[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

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,
0
Antonio King
Asked:
Antonio King
  • 3
  • 2
  • 2
1 Solution
 
basicinstinctCommented:
Copy the code below, past to empty text file, change the obvious three constants at the start of the file to suit your needs, save file as "whatever.vbs", run it.

'START CODE
Const SEARCH_STRING = "XXXXX" 'The signature will be appended immediately after this string
Const SIGNATURE = "<br>www.xxxxxxx.com" 'This is the signaute that will be added
Const TARGET_FOLDER = "C:\Folder\Where\htm\files\are" 'The location that will be searched for files to update

Const FILE_EXT = "htm" 'The file type to update with signature
const FOR_READING = 1
Const FOR_WRITING = 2

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")

Dim CurrentFolder: Set CurrentFolder = fso.GetFolder(TARGET_FOLDER)
AddSig CurrentFolder

Sub AddSig (ByVal Folder)
      Dim ts
      Dim s
      Dim File
      Dim Files: Set Files = Folder.Files
      For Each File In Files
             If LCase(fso.GetExtensionName(File)) = FILE_EXT Then
             Set ts = fso.OpenTextFile(File, FOR_READING)
            If Not ts.AtEndOfStream Then
                  s = ts.ReadAll
                  ts.Close
                  s = Replace(s, SEARCH_STRING, SEARCH_STRING & VBCRLF & SIGNATURE)
                  Set ts = fso.OpenTextFile(File, FOR_WRITING, True)
                  ts.Write(s)
                  ts.Close
            End If
       End If
      Next
End Sub
'END CODE
0
 
fridomCommented:
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
0
 
Antonio KingIT ManagerAuthor Commented:
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!

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
fridomCommented:
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
0
 
basicinstinctCommented:
might not do what you wanted - does exactly what you asked for
0
 
Antonio KingIT ManagerAuthor Commented:
@BasicInstinct...

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

Thanks!
0
 
basicinstinctCommented:
oh cool, good stuff, cheers Alan
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now