1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Replace a string in multiple files

Discussion in 'C#' started by d3scr1pt0r, Aug 14, 2008.

  1. #1
    I wanted to replace a certain string (an adsense id) in thousands of files, so I thought doing it in vb.net.

    This is not a question, I found the solution. This is just some code that does the replacing for people that want to put it in future projects :)

    I assume you have a textbox named "adsense_txt.Text" that contains the target adsense id
    Also I assume that you have assigned to the variable "copiedmiscfile2" the path to the file you want the replacing to be done.

    You just need to loop through all your files this code that will read the file line by line until it finds a match and replace it:)

     
    'replace target adsense with the curent adsense pub-xxxxxxxxx
    Dim rx5 As New Regex("pub-tobereplaced", RegexOptions.IgnoreCase)
    Dim filename5 As String = copiedmiscfile2
    Dim sr5 As New StreamReader(filename5)
    Dim source5() As String = sr5.ReadToEnd.Replace(Environment.NewLine, Chr(13)).Split(Chr(13).ToString.ToCharArray)
    sr5.Close()
    Dim Changes5 As New List(Of Integer)
    For a As Integer = 0 To source5.Length - 1
    If rx5.IsMatch(source5(a)) Then
    source5(a) = rx5.Replace(source5(a), adsense_txt.Text)
    Changes5.Add(a + 1)
    End If
    Next
    Dim sw5 As New StreamWriter(filename5)
    sw5.Write(String.Join(Environment.NewLine, source5))
    sw5.Close()
    
    Code (markup):
     
    d3scr1pt0r, Aug 14, 2008 IP