Replacing multiple text strings within a file

Mnemonic

Member
Joined
May 24, 2007
Messages
9
Programming Experience
1-3
Hello guys and gals,

I was recently tasked with the really, really stupid job of rewriting IIS logs to show the hits such as "/home/sites/webpart1/default.aspx" as "/home/Default-Web-Part".. a.k.a URL "Friendly names" for clients to understand.

so i decided to make a lightweight application that will extract the data inside a log file and rewrite it to a different directory(for reasons of there are other sites in the log file that i dont want to risk messing with their data).

The problem im running into is replacing the text in vb. I have many different url strings i need to replace, and im not really sure how to do it. So far i got this working with this code:

VB.NET:
        Dim _24HoursAgo As Date = Date.Now.AddHours(-900)
        For Each file1 As IO.FileInfo In (New IO.DirectoryInfo("c:\testlog\")).GetFiles("ex080214.LOG_2")
            If file1.LastAccessTime >= _24HoursAgo Then
                Dim strfile As String = file1.Name
                Dim reader1 As String
                Dim reader As StreamReader = File.OpenText("c:\testlog\" & strfile)
                Dim contents As String = reader.ReadToEnd()
                Dim output As String
                output = contents
                Dim objnew**** As String
                objnew**** = contents.Replace("/home/sites/webpart1/default.aspx", "home/Default-Landing-Page")

                Dim strOutputFile = "c:\testlog2\" & strfile

                Dim objFileSystem = CreateObject("Scripting.fileSystemObject")
                Dim objOutputFile = objFileSystem.CreateTextFile(strOutputFile, True)


                objOutputFile.Close()

                objFileSystem = Nothing
                objOutputFile = Nothing



                Dim objwriter As StreamWriter
                objwriter = File.AppendText("c:\testlog2" & strfile)
                Const OPEN_FILE_FOR_APPENDING = 8

                ' generate a filename base on the script name
                Dim strOutputFile2 = "c:\testlog2\" & strfile

                objFileSystem = CreateObject("Scripting.fileSystemObject")
                objOutputFile = objFileSystem.OpenTextFile(strOutputFile2, _
                  OPEN_FILE_FOR_APPENDING)
                objwriter.Flush()
                objOutputFile.WriteLine(objnew****)
                objOutputFile.Close()

                objFileSystem = Nothing
                reader.Close()

                MsgBox(objnew****)
            End If
        Next file1
Any ideas on how i can accomplish what i need to do?
 
have a big 2xN array of find/replace strings (or put them in an xml file and read it into a dataset, even better)

VB.NET:
<pair>
  <find>findme</find>
  <repl>replaceMe</repl>
</pair>

VB.NET:
Dim ds as new DataSet
ds.ReadXml(":c:\filepath")

Dim sb as New stringbuilder(iisLogFileAsString)
For Each ro as DataRow in ds.Tables("pair")
  sb.Replace(ro("find").ToString(). ro("repl").ToString())
Next ro
 
Really cool idea, but im running into one small problem:

On this line:

For Each ro As DataRow In ds.Tables("pair") <--- Get this exception underlined on "pair".

Expression is of type 'System.Data.DataTable', which is not a collection type xml
 
Sorry. I usually work with typed data tables (cant recommend them highly enough) which have a default property .Rows thus:

myDataSet.myTypedDataTable(5) 'returns the sixth row


Untyped datatables need to use the .Rows property:

myDataSet.Tables("myUntypedDatatable").Rows(5)



Thus you needs to add the .Rows property to get access to the row collection:

For Each ... In myDataSet.Tables("...").Rows
 
Back
Top