• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

RegEx.Pattern trouble writing to txt file

If I use the function
' ------------
Function RemoveHTML( varCopy )
      Dim RegEx

      Set RegEx = New RegExp

      RegEx.Pattern = "<[^>]*>"
      RegEx.Global = True

      RemoveHTML = RegEx.Replace(varCopy, "")
End Function
newVarCopy = RemoveHTML(varCopy)
' ---------------
and response.write the results to the browser, I have no problems. But, If I try to write to a txt file, I get the error message of:

Microsoft VBScript compilation  error '800a03ea'
Syntax error
/pricerunner/index2.asp, line 32
Function RemoveHTML( varCopy )
^

I am using
' -----------------
' varCopy is already defined from rs
file_being_created= "pricerunner_" & Replace(FormatDateTime(Date(),2),"/","") & ".txt"
set fso = createobject("scripting.filesystemobject")
Set act = fso.CreateTextFile(server.mappath(file_being_created), true)

Function RemoveHTML( varCopy )
      Dim RegEx

      Set RegEx = New RegExp

      RegEx.Pattern = "<[^>]*>"
      RegEx.Global = True

      RemoveHTML = RegEx.Replace(varCopy, "")
End Function

strLine = "first line" & Chr(13)
strLine = strLine & "second line" & Chr(13)
strLine = strLine & RemoveHTML(varCopy) & Chr(13)

act.WriteLine(strLine)
' --------------------

is this not a valid use of this function? Is there another way to get the results I am looking for? Basically, I am taking descriptive product copy that contains html tags and am trying to strip the tags out and output to a text file for a datafeed. I can do this with a series of Replace statements but am concerned about missing some.

Thanks!
0
shadie
Asked:
shadie
  • 6
  • 4
  • 3
1 Solution
 
WMIFCommented:
try using .write instead of .writeline.  you may be running against the line breaks in your string or something.
0
 
kevp75Commented:
i've run into this before.

What I did, was moved the function out of the code and into an included file.

@WMIF, doesn't .writeline write a line to the text file? and .write is for display only?
0
 
WMIFCommented:
writeline puts whatever you pass into the text file and writes a crlf after that data.  

write puts what you pass only.

http://www.w3schools.com/asp/asp_ref_textstream.asp
scroll down to methods.
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.

 
kevp75Commented:
ahh.

Now, will .write recognize a line break if it is coded into what is to be written?
0
 
WMIFCommented:
it wont recognize anything, it just writes to the file whatever it gets.  by recognize do you mean, will the break be written to the file?  if so, then yes it will be written just as its passed.
0
 
kevp75Commented:
yes WMIF, that is what I meant by recognize...

so...would it really make a difference if the asker user .write as opposed to .writeline...   seems that .writeline would just put in a line break after the data is written along with the line breaks in the existing code., and .write would just skip out on that last line break?

@shadie
without building a db to pull data from, I've tried your code, and it seems that moving the function is the way to go.  Basically all I did was:

dim varCopy
varCopy = "<html><body><font face="verdana" size="10">Some text<br>to write</font></body></html>"
Function RemoveHTML( varCopy )
     Dim RegEx

     Set RegEx = New RegExp

     RegEx.Pattern = "<[^>]*>"
     RegEx.Global = True

     RemoveHTML = RegEx.Replace(varCopy, "")
End Function
file_being_created= "pricerunner_" & Replace(FormatDateTime(Date(),2),"/","") & ".txt"
set fso = createobject("scripting.filesystemobject")
Set act = fso.CreateTextFile(server.mappath(file_being_created), true)

strLine = "first line" & Chr(13)
strLine = strLine & "second line" & Chr(13)
strLine = strLine & RemoveHTML(varCopy) & Chr(13)

act.WriteLine(strLine) 'changing this to .write has the same basic effect, except .writeline put in an extra line break
0
 
WMIFCommented:
>>so...would it really make a difference if the asker user .write as opposed to .writeline...  

i dont know, that is why i suggested that the asker try it.  i dont have a server available to me as of right now to test.  apparently you did and answered the question.


@shadie - something else i just caught could be the use of similar variable names.  try changing the argument of your function to have a different variable name.

Function RemoveHTML( inputCopy )
RemoveHTML = RegEx.Replace(inputCopy, "")
0
 
shadieAuthor Commented:
kevp,
I call this the real estate function -- because it's all about location, location, location...

I was all set to give you the nod for your answer as I changed the location of that function and the page loaded without errors. It produced the file and I thought I was done.

I then went into the data and found that moving the function to before I declare the filesystemobject, that that also removed the "dim varCopy" to outside of the rs loop. So all the records output to the file are the same record instead of the loop. Any thoughts?
0
 
kevp75Commented:
you mention loop, I do not see one posted in your code above.    Please post it.
0
 
shadieAuthor Commented:
rs select HERE..

file_being_created= "pricerunner_" & Replace(FormatDateTime(Date(),2),"/","") & ".txt"
set fso = createobject("scripting.filesystemobject")
Set act = fso.CreateTextFile(server.mappath(file_being_created), true)

strLine = "category||sku||price||product URL||product name||product state||manufacturer SKU||manufacturer||in stock||stock level||delivery time||shipping cost||description||craphic URL"
strLine = strLine & Chr(13)

while not rs.eof

dim varCopy
varCopy = rs("copy")
Function RemoveHTML( varCopy )
     Dim RegEx

     Set RegEx = New RegExp

     RegEx.Pattern = "<[^>]*>"
     RegEx.Global = True

     RemoveHTML = RegEx.Replace(varCopy, "")
End Function

if varCopy <> "" then
-- a bunch of other strLine = strLine & ... HERE

end if
rs.movenext
wend

act.WriteLine(strLine)
0
 
kevp75Commented:
that looks like you didn't move anything?

move the dim varCopy, and the function to the top of your page, keep varCopy = rs("copy") where it is
0
 
shadieAuthor Commented:
oooof -- I can be such an idiot sometimes!

I did move the code per your previous post and found that it worked without error but found that no-loop problem. I put the function back in the loop and that's where I commented last. When I sent you over that last block of code, it was the version with the function in the loop, not outside.

Regardless, I moved the function to the top of the page and variable declaration. I followed up with the rs and FSO then started my writeLine loop and it looks like all is well.

Thanks for the help!
0
 
kevp75Commented:
np...glad I could help
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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