Find a string and replace it more effeciently - asp.net

Situation: I have a html file and I need to remove certain sections.
For Example: The file contains html: <div style="padding:10px;">First Name:</div><div style="padding:10px; background-color: gray">random information here</div><div style="padding:10px;">First Name:</div><div style="padding:10px; background-color: gray">random information here</div>
I need to remove all text that starts with "<div style="padding:10px; background-color: gray">" and ends with "</div>" so that the result would be:
<div style="padding:10px;">First Name:</div><div style="padding:10px;">First Name:</div>
I created 2 functions that do this, but I do not this it efficient at all. I have a 40mb file and it takes the program about 2 hours to complete. Is there a more efficient way to do this? Is there a way to use regex?
See my code below:
Public Shared Function String_RemoveText(ByVal startAt As String, ByVal endAt As String, ByVal SourceString As String) As String
Dim TotalCount As Integer = String_CountCharacters(SourceString, startAt)
Dim CurrentCount As Integer = 0
RemoveNextString:
Dim LeftRemoved As String = Mid(SourceString, InStr(SourceString, startAt) + 1, Len(SourceString) - Len(endAt))
Dim RemoveCore As String = Left(LeftRemoved, InStr(LeftRemoved, endAt) - 1)
Dim RemoveString As String = startAt & RemoveCore & endAt
Do
' Application.DoEvents()
SourceString = Replace(SourceString, RemoveString, "")
If InStr(SourceString, startAt) < 1 Then Exit Do
GoTo RemoveNextString
Loop
Return Replace(SourceString, RemoveString, "")
End Function
Public Shared Sub Files_ReplaceText(ByVal DirectoryPath As String, ByVal SourceFile As String, ByVal DestinationFile As String, ByVal sFind As String, ByVal sReplace As String, ByVal TrimContents As Boolean, ByVal RemoveCharacters As Boolean, ByVal rStart As String, ByVal rEnd As String)
'CREATE NEW FILENAME
Dim DateFileName As String = Date.Now.ToString.Replace(":", "_")
DateFileName = DateFileName.Replace(" ", "_")
DateFileName = DateFileName.Replace("/", "_")
Dim FileExtension As String = ".txt"
Dim NewFileName As String = DirectoryPath & DateFileName & FileExtension
'CHECK IF FILENAME ALREADY EXISTS
Dim counter As Integer = 0
If IO.File.Exists(NewFileName) = True Then
'CREATE NEW FILE NAME
Do
'Application.DoEvents()
counter = counter + 1
If IO.File.Exists(DirectoryPath & DateFileName & "_" & counter & FileExtension) = False Then
NewFileName = DirectoryPath & DateFileName & "_" & counter & FileExtension
Exit Do
End If
Loop
End If
'END NEW FILENAME
'READ SOURCE FILE
Dim sr As New StreamReader(DirectoryPath & SourceFile)
Dim content As String = sr.ReadToEnd()
sr.Close()
'WRITE NEW FILE
Dim sw As New StreamWriter(NewFileName)
'REPLACE VALUES
content = content.Replace(sFind, sReplace)
'REMOVE STRINGS
If RemoveCharacters = True Then content = String_RemoveText(rStart, rEnd, content)
'TRIM
If TrimContents = True Then content = Regex.Replace(content, "[\t]", "")
'WRITE FILE
sw.Write(content)
'CLOSE FILE
sw.Close()
End Sub
Example to execute the code (also removes Chr(13) & Chr(10):
Files_ReplaceText(tPath.Text, tSource.Text, "", Chr(13) & Chr(10), "", True, True, tStart.Text, tEnd.Text)

Do not use a RegEx to parse HTML - it is not a regular language. See here for some compelling demonstrations.
Use the HTML Agility Pack to parse the HTML and replace data.

Related

How can I stop Ionic zip from appending null to the end of a file

I have a VB.net (ASPX) which receives a file (data stream) from my application, and depending on switches will save the file as is or compress & encrypt it using ionic.zip. When a file is saved without encryption it is byte perfect. When the same file is compressed/encrypted and then decrypted/uncompressed there is an extra null (ascii(0)) character appended to the end of the decrypted file. This is generally not an issue, but MS Office products complain about corrupted files (then opens them fine).
The code I use is fairly straightforward and I cannot see any issues. I found nothing in my searches for this issue with ionic zip. Here is the code;
<%# Page Language="VB" validateRequest="false"%>
<%# Import Namespace="Ionic.Zip"%>
<%# Import Namespace="System.Web.HttpContext"%>
<script Runat="Server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim strToken As String = Request.QueryString("token")
Dim strSessionID As String = Request.QueryString("SessionID")
Dim strAlternateFileName As String = Request.QueryString("AlternateFileName")
Dim strDestinationFolder As String = Request.QueryString("DestinationFolder")
Dim strZipYN As String = Request.QueryString("ZipYN")
Dim strZipFileName As String = Request.QueryString("ZipFileName")
Dim strZipPassword As String = Request.QueryString("ZipPassword")
Dim fileName As String = System.IO.Path.GetFileName(Request.Files(0).FileName)
If IsNothing(Current.Session("token" & strSessionID)) _
Then
Current.Session("token") = strToken
Current.Session("token" & strSessionID) = Current.Session("token")
End If
If Len(strAlternateFileName) > 0 _
Then
fileName = strAlternateFileName
End If
If strZipYN <> "Y" _
Then
Request.Files(0).SaveAs(Server.MapPath(strDestinationFolder + "/") + fileName)
Else
Dim fileIn As HttpPostedFile
Dim objZipFile As ZipFile = New ZipFile
fileIn = Request.Files(0)
Dim intFileInLen = fileIn.ContentLength
Dim bytFielIn(intFileInLen) As Byte
Dim strmFileIn As System.IO.Stream
strmFileIn = fileIn.InputStream
strmFileIn.Read(bytFielIn, 0, intFileInLen)
If strZipPassword.Length > 0 _
Then
objZipFile.Password = strZipPassword
objZipFile.Encryption = EncryptionAlgorithm.WinZipAes256
End If
objZipFile.AddEntry(fileName, bytFielIn)
objZipFile.SaveSelfExtractor(Server.MapPath(strDestinationFolder + "/") + strZipFileName + ".exe", SelfExtractorFlavor.WinFormsApplication)
' objZipFile.SaveSelfExtractor(Server.MapPath(strDestinationFolder + "/") + strZipFileName + ".zip", SelfExtractorFlavor.WinFormsApplication)
objZipFile.Dispose()
objZipFile = Nothing
'Create a PW protected ZIP only for non windows computers.
objZipFile = New ZipFile
If strZipPassword.Length > 0 _
Then
objZipFile.Password = strZipPassword
objZipFile.Encryption = EncryptionAlgorithm.WinZipAes256
End If
objZipFile.AddEntry(fileName, bytFielIn)
objZipFile.Save(Server.MapPath(strDestinationFolder + "/") + strZipFileName + ".zip")
objZipFile.Dispose()
objZipFile = Nothing
End If
End Sub
</script>
Any ideas?
Edit:
I get the same results whether I extract the file from the ".exe" file, or the ".zip" file

How to call function from class

I am trying to load a function value into a Literal2.Text.
I am getting the error
ErrorArgument not specified for parameter 'LoadMenu' of 'Public
Function LoadMenuActivity(LoadMenu As String) As String'
I call the function on page load like this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Literal2.Text = AllFunc.LoadMenuActivity
End If
End Sub
Here is my Class:
Public Class AllFunc
Public Function LoadMenuActivity(ByVal LoadMenu As String) As String
Dim strCON As String = "Data Source=localhost;Initial Catalog=TAPVendor;Integrated Security=True"
Dim strSQL = "SELECT * FROM dbo.tbl_Message WHERE UserID = 'RAN' ORDER BY ID DESC"
Dim da As New SqlClient.SqlDataAdapter(strSQL, strCON)
Dim dt As New DataTable
da.Fill(dt)
Dim display As String = Nothing
Dim sb As StringBuilder = New StringBuilder()
Dim counter As Integer = Nothing
For i As Integer = 0 To dt.Rows.Count - 1
counter = counter + 1
Dim MyString As String
MyString = dt.Rows(i).Item("Timestamp")
Dim MyDateTime As DateTime
MyDateTime = New DateTime
MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm:ss tt", Nothing)
Dim t As TimeSpan = DateTime.Now - MyDateTime
If t.TotalSeconds > 1 Then
display = t.Seconds.ToString() + " sec ago"
End If
If t.TotalSeconds > 60 Then
display = t.Minutes.ToString() + " mins ago"
End If
If t.TotalHours > 1 Then
display = t.Hours.ToString() + " hrs ago"
End If
If t.TotalDays > 1 Then
display = t.Days.ToString() + " days ago"
End If
sb.AppendFormat("<li class=""divider""></li>" &
" <li><a href=""#"">" &
"<div>" &
"<i class=""" & dt.Rows(i).Item("Icon") & """></i> " & dt.Rows(i).Item("Alert") & "" &
"<span class=""pull-right text-muted small"">" & display & "</span></div></a></li>")
If counter = 5 Then
Exit For
End If
Next
Return LoadMenu
End Function
End Class
What am I doing wrong?
Like the error message says, you didn't specify an argument when calling LoadMenuActivity. You have to call LoadMenuActivity("Some string").
The argument inside the () is requiring a string to be passed to it. Plus that function needs to be a Shared function to call it that way.
Literal2.Text = AllFunc.LoadMenuActivity("some string here")

VBA Code to Copy and Paste Excel Range into Outlook

I need to copy a range from an Excel file into Outlook, then send it as an email. It needs to be embedded into the email itself. I found this code which works great, with one exception: It is centering the range in the middle of the "page" in outlook, and I need it to align to the left.
I am assuming this is done in HTML but I do not know that language. Here is the code I am using:
Option Explicit
Public Sub prcSendMail()
Dim objOutlook As Object, objMail As Object
Set objOutlook = CreateObject(Class:="Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
With objMail
.To = "Mike.Marshall#worldpay.us"
.Subject = "Hallo"
.HTMLBody = fncRangeToHtml("Summary", "B2:G26")
.Display 'zum testen
' .Send
End With
Set objMail = Nothing
Set objOutlook = Nothing
End Sub
Private Function fncRangeToHtml( _
strWorksheetName As String, _
strRangeAddress As String) As String
Dim objFilesytem As Object, objTextstream As Object, objShape As Shape
Dim strFilename As String, strTempText As String
Dim blnRangeContainsShapes As Boolean
strFilename = Environ$("temp") & "\" & _
Format(Now, "dd-mm-yy_h-mm-ss") & ".htm"
ThisWorkbook.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=strFilename, _
Sheet:=strWorksheetName, _
Source:=strRangeAddress, _
HtmlType:=xlHtmlStatic).Publish True
Set objFilesytem = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFilesytem.GetFile(strFilename).OpenAsTextStream(1, -2)
strTempText = objTextstream.ReadAll
objTextstream.Close
For Each objShape In Worksheets(strWorksheetName).Shapes
If Not Intersect(objShape.TopLeftCell, Worksheets( _
strWorksheetName).Range(strRangeAddress)) Is Nothing Then
blnRangeContainsShapes = True
Exit For
End If
Next
If blnRangeContainsShapes Then _
strTempText = fncConvertPictureToMail(strTempText, Worksheets(strWorksheetName))
fncRangeToHtml = strTempText
Set objTextstream = Nothing
Set objFilesytem = Nothing
Kill strFilename
End Function
Public Function fncConvertPictureToMail(strTempText As String, objWorksheet As Worksheet) As String
Const HTM_START = "<link rel=File-List href="
Const HTM_END = "/filelist.xml"
Dim strTemp As String
Dim lngPathLeft As Long
lngPathLeft = InStr(1, strTempText, HTM_START)
strTemp = Mid$(strTempText, lngPathLeft, InStr(lngPathLeft, strTempText, ">") - lngPathLeft)
strTemp = Replace(strTemp, HTM_START & Chr$(34), "")
strTemp = Replace(strTemp, HTM_END & Chr$(34), "")
strTemp = strTemp & "/"
strTempText = Replace(strTempText, strTemp, Environ$("temp") & "\" & strTemp)
fncConvertPictureToMail = strTempText
End Function
Is there some code to left align the range I am copying into Outlook?
I have W7 x64, Excel 2013 and Outlook 2013.
Thanks!
add this after your objTextstream.Close
strTempText = Replace(strTempText, "align=center x:publishsource=", "align=left x:publishsource=")
This worked for me
With objMail
.To = "Bofa#deeznutz.com"
.cc = ""
.Subject = "BR1 Summary for Adjustments +/- >$250"
.HTMLBody = "<table width='100'><tr><td align=left>" + fncRangeToHtml("weekly adjustments report", Sheet1.UsedRange.Address) + "</td></tr></table>" & "<br>" & "<b>" & "<font size=4>" & "Adjustments +/- >$250" & "</font>" & "</b>" & fncRangeToHtml("Sheet1", Sheet2.UsedRange.Address)
VBA likes the quotes and the spaces. but in that last line of code you can either quote all of you HTML functions or break it up. but once you are finished using that like bold, you have to "/function" to end it before it likes the information. the & and + work the same.

how to use Word Object Model to programmatically replace text in a Word doc

My function needs to read an MS Word doc, replace some text, and then SAVE AS PDF.
Reading and saving PDF works great.
But I can't figure out how to replace text by using Word Object Model.
Here is my word_to_PDF() function which does it all.
Function word_to_PDF(Word_source_filepath, PDF_destin_filepath)
Dim wordApplication As ApplicationClass = New ApplicationClass()
Dim wordDocument As Document = Nothing
Dim paramExportFilePath As String = PDF_destin_filepath
Dim paramExportFormat As WdExportFormat = _
WdExportFormat.wdExportFormatPDF
Dim paramOpenAfterExport As Boolean = False
Dim paramExportOptimizeFor As WdExportOptimizeFor = _
WdExportOptimizeFor.wdExportOptimizeForPrint
Dim paramExportRange As WdExportRange = _
WdExportRange.wdExportAllDocument
Dim paramStartPage As Int32 = 0
Dim paramEndPage As Int32 = 0
Dim paramExportItem As WdExportItem = _
WdExportItem.wdExportDocumentContent
Dim paramIncludeDocProps As Boolean = True
Dim paramKeepIRM As Boolean = True
Dim paramCreateBookmarks As WdExportCreateBookmarks = _
WdExportCreateBookmarks.wdExportCreateWordBookmarks
Dim paramDocStructureTags As Boolean = True
Dim paramBitmapMissingFonts As Boolean = True
Dim paramUseISO19005_1 As Boolean = False
' Open the source document.
On Error Resume Next
wordDocument = wordApplication.Documents.Open(Word_source_filepath) ' Microsoft.Office.Interop.Word.Document
If Err.Number Then
Debug.WriteLine(Err.Description)
log_fault("word_to_PDF: Err# " & Err.Number & " " & Err.Description)
Stop '!!!
Else
' Export it in the specified format.
If Not wordDocument Is Nothing Then
Dim FindObject As wordDocument.Find = Application.Selection.Find <<<<<<<<<<<<<<< THIS GETS ERROR: "wordDocument.Find is not defined"
With FindObject
.ClearFormatting()
.Text = "find me"
.Replacement.ClearFormatting()
.Replacement.Text = "Found"
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
wordDocument.ExportAsFixedFormat(paramExportFilePath, _
paramExportFormat, paramOpenAfterExport, _
paramExportOptimizeFor, paramExportRange, paramStartPage, _
paramEndPage, paramExportItem, paramIncludeDocProps, _
paramKeepIRM, paramCreateBookmarks, _
paramDocStructureTags, paramBitmapMissingFonts, _
paramUseISO19005_1)
Else
'!!! ERR
stop_if_debug()
End If
End If
' Close and release the Document object:
If Not wordDocument Is Nothing Then
wordDocument.Close(False)
wordDocument = Nothing
End If
' Quit Word and release the ApplicationClass object.
If Not wordApplication Is Nothing Then
wordApplication.Quit()
wordApplication = Nothing
End If
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
Return True
Change your wordDocument.Find to just Word.Find.
Find is an interface on the Word namespace but not an object on the instanced class.
Here's some msdn documentation about what you are trying to do:
http://msdn.microsoft.com/en-us/library/f1f367bx.aspx
What's in the Word namespace
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word%28v=office.14%29.aspx

compare 2 string to get result different

i am doing a method that able to return me what is the different between data before and data after
Dim dataBefore As String = "Name:Alice,Age:30,Sex:Male"
Dim dataAfter As String = "Name:Alice,Age:20,Sex:Female"
mtdCompare2String(dataBefore,dataAfter)
Public Shared Function mtdCompare2String(ByVal sBefore As String, ByVal sAfter As String) As String
//what i try to do before, supposing my loop should start over here but i failed,
//so i just removed the loop, i need someone to correct me =(
Dim intBefore As Integer = sBefore.IndexOf(",")
Dim intAfter As Integer = sAfter.IndexOf(",")
Dim sBefore As String = sBefore.SubString(0,intBefore)
Dim sAfter As String = sAfter.SubString(sAfter.IndexOf(":"),intAfter)
Dim sb As StringBuilder
sb.append(sBefore,sAfter)
return sb.toString
End Function
Expected Result
Age:30>20,Sex:Male>Female
Something like this should work:
Public Shared Function mtdCompare2String(ByVal sBefore As String, ByVal sAfter As String) As String
'what i try to do before, supposing my loop should start over here but i failed,
'so i just removed the loop, i need someone to correct me =(
Dim Before() As String = sBefore.Split(",")
Dim After() As String = sAfter.Split(",")
Dim ReturnString As String = ""
For I = 0 To Before.Length - 1
Dim TempBefore As String = Before(I).Split(":")(1)
Dim TempAfter As String = After(I).Split(":")(1)
If TempBefore <> TempAfter Then
ReturnString += Before(I).Split(":")(0) + ":" + TempBefore + ">" + TempAfter + ","
End If
Next
Return ReturnString.Substring(0, ReturnString.Length - 1)
End Function

Resources