Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replacing lines containing / < etc

Status
Not open for further replies.

CalYu

Technical User
Oct 7, 2008
3
Hello there,

Totally new to VBS scripts, my program is not elegant (and its the third attempt). I've searched for the solution for what i want to do - which should seem obvious but ...nothing.

In a nutshell i want to assign something like this
ins3 = ".jpg></A></i></h1><table border="
and be able to do a replace for
HtmlFile = Replace(HtmlFile,"</i></h1><table border=",name3)

Purpose is to insert a jpg (or whatever) file in a large (like 7000) number of html files, each of the htmls contains a table - that's where the pic should go.

Thanks in advance

This is the code

On Error Resume Next
Dim ArrAll()

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
Const ForReading = 1
Const ForAppending = 8
Const ForWritting = 2
name2 = ".html"
Ins1 = "<IMG SRC ="
'*****************************************
ins3 = ".jpg></A></i></h1>
'*****************************************
<table border="

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

strPathtoTextFile = "C:\1\"

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""

objRecordset.Open "SELECT * FROM ZZ.csv", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText

i=0
'populate the array with information
Do Until objRecordset.EOF
Redim Preserve ArrAll(i)
ArrAll(i) = objRecordset.Fields.Item("NAME")
i=i+1
objRecordset.MoveNext
Loop
objRecordset.Close

'use array to open whatever file and write info
For i = 1 to Ubound(ArrAll)
name1=ArrAll(i) & name2
name3=Ins1 & ArrAll & ins3
Set objFile = objFSO.OpenTextFile(name1, ForReading)
HtmlFile = objFile.ReadAll
objfile.Close
'*****************************************
HtmlFile = Replace(HtmlFile,"</i></h1>
<table border=",name3)
'*****************************************
Set objFile = objFSO.OpenTextFile(name1, ForWriting)
objFile.WriteLine HtmlFile
objFile.Close
Next
 
Is this the problem?
name3=Ins1 & ArrAll(i) & ins3

Also, for VBScript questions you should use forum329. This is for VB 5 & 6.
 
Hi Guitarzan

Realize I didn't spell out the problem very clearly - oh it all seemed perfectly logical to me before, but then i went and included the background/program. I'll remember to post there next time, just saw VB and thought this is the place.

Anyhow - prob is when doing the following
ins3 = ".jpg></A></i></h1>"
or
HtmlFile = Replace(HtmlFile,"</i></h1> <table border=",name3)

I receive an error for unterminated string constant. And then I curse at the program for not believing that it really is a string.
 
In your code example, that line of code is broken into two lines:
HtmlFile = Replace(HtmlFile,"</i></h1>
<table border=",name3)

This will cause the unterminated string constant error. Should be:

HtmlFile = Replace(HtmlFile,"</i></h1><table border=",name3)
 
If you need it to search for something that is on two lines you can change it to this:

HtmlFile = Replace(HtmlFile,"</i></h1>" & vbcrlf & "<table border=",name3)
 
Thanks for your input guys - the error messages I received made it seem to me that VBS was not able to put characters such as "/>" etc, let alone if you combined them with variables...that's what i was trying to explain (badly).

Especially when the first variable ie

ins3 = ".jpg></A></i></h1> <table border="
was stuffing up, maybe the memory wasn't being cleared (both stuff ups were on 1 line btw, just copy/paste had ideas of its own.

In any case i started from scratch line by line i got it to work. The following is the result, thanks again for your time.

On Error Resume Next
Dim ArrAll()
Const ForReading = 1
Const ForWriting = 2
E1 = ".html"
E2 = ".jpg"

Set objFSO = CreateObject("Scripting.FileSystemObject")
txtin = "c:\1\ZZ0.csv"

Set objTextFile = objFSO.OpenTextFile _
(txtin, ForReading)
i=1
Do Until objTextFile.AtEndOfLine
Redim Preserve ArrReadLine(i)
ArrReadLine(i) = objTextFile.Readline
i=i+1
Loop

objTextFile.close

'use array to open whatever file and write info
For i = 1 to Ubound(ArrReadLine)
name1 = ArrReadLine(i) & E1
Set objFile = objFSO.OpenTextFile(name1, ForReading)
HtmlFile = objFile.ReadAll
objfile.Close
'splits the "name_name" to "name name"
ArrSplitLine = Split(ArrReadLine(i), "_")
'formats the bits to search and replace
rep1 = ArrSplitLine(1) & "</i></h1>"
ins1 = "<img src="
ins2 = ArrReadLine(i) & E2
ins3 = ">"
insall = rep1 & ins1 & ins2 & ins3

'Replaces "name </i></h> with name</i></h><img src=name_name.jpg>
HtmlFile2 = Replace(HtmlFile,rep1,insall)
Set objFile = objFSO.OpenTextFile(name1, ForWriting)
objFile.WriteLine HtmlFile2
objFile.Close
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top