Friday, February 5, 2010

C#.net - Find and Replace in large files

Problem-Scenario: When we are working on large files (e.g. > 1GB) and you have to do simple operation like Replace a string with another string.
regex.replace or string.replace or using xmldom object or LINQ doesn't work! And there are no free tools in market which does it without throwing up in middle.
  
Solution:
Well the solution is as simple as it can get. Just use StreamReader & StreamWriter. These don't load file in memory but streams through your text or xml file byte by byte

Example: (Change the required parameter values for changing a site's site definition)

            /// Replaces text in a file.
            ///
Path of the text file.
            ///Text to search for.
            ///Text to replace the search text.
            

public void ReplaceInFile(string SourcefilePath, string DestfilePath)
            {
               string data;
                if( !(SourcefilePath.Contains(".xml")) )
                {
                    Console.WriteLine("Please specify Manifest.xml path.. Filename is missing");
                    return;
                }
               if(File.Exists(SourcefilePath) == false)
               {
                   Console.WriteLine("File doesn't exist at the specified path\n");
                   return;
               }
              

            StreamReader streamReader = new StreamReader(SourcefilePath);
            StreamWriter streamWriter = new StreamWriter(DestfilePath);

            while (streamReader.Peek() >= 0)
            {

                data = streamReader.ReadLine();

                //**********************************
                // Strings for changing the Configuration IDs
                string OldConfig1 = @"Configuration=""0""";
                string NewConfig = @"Configuration=""2""";

                //-1. Webtemplate="InsideCustompublishingWorkflow - Config as -1 change it to 2
                string searchtext23 = @"WebTemplate=""InsideCustompublishingWorkflow""";

                if (data.Contains(searchtext23) == true)
                {
                    //change the configuration
                    data = data.Replace(OldConfig1, NewConfig);
                 }

           
                //CHANGE THE SITE TEMPLATE NAME
                // 1. WebTemplate="INSIDECustomPUBLISHING" - Aold
                string searchtext1 = @"WebTemplate=""INSIDECustomPUBLISHING""";
                string replacetext1 = @"WebTemplate=""INSIDECustomPUBLISHINGnew""";

                if (data.Contains(searchtext1) == true)
                {
                    //change the configuration
                    data = data.Replace(OldConfig1,NewConfig);
                    data = data.Replace(searchtext1, replacetext1);
                   
                }
               

                //CHANGE THE SITE TEMPLATE SETUPPATH
                //
INSIDECustomPUBLISHING - Aold //SetupPath="SiteTemplates\INSIDECustomPUBLISHING"
                string searchtext4 = @"SetupPath=""SiteTemplates\
INSIDECustomPUBLISHING\";
                string replacetext4 = @"SetupPath=""SiteTemplates\
INSIDECustomPUBLISHINGnew\";
                data = data.Replace(searchtext4, replacetext4);


                //Write the data on .xmlnew file
                streamWriter.WriteLine (data);

            }

            streamReader.Close();
            streamWriter.Close();
}

 

No comments: