Search and Replace strings in files

SearchAndReplaceThe other day I ran into a problem where I needed to change the name of an URL for a website which was hard-coded in a bunch of legacy asp code. Now usually I would use a tool such as EditPlus, use the Find in Files function, open the matching files and use search and replace to update them. But in this case there were hundreds of files affected and I didn’t really want to open hundreds of files in a single EditPlus session so instead I searched around the net looking for an easy Search and Replace program. I didn’t really find anything I liked so I thought I’d create a simple utility.

Search and Replace is a windows program which searches folders and subfolders for a specified string in a list of files and replaces that string if desired. Here is the interface I came up with:

Search and Replace GUI

Here I’ve searched for “Badda Bing!” in all text files in my test folder and I included subfolders. When a match is found the name of the file is displayed and when the search completes the Results label is updated to display the files found. Note that multiple occurrences are only displayed once.

Here is the main loop which does all the work:

foreach (string fileName in filenames)

{

   // Open a file for reading

   StreamReader streamReader;

   streamReader = File.OpenText(fileName);

   // Now, read the entire file into a string

   string contents = streamReader.ReadToEnd();

   streamReader.Close();

 

   if (Regex.IsMatch(contents, txtFind.Text, RegexOptions.IgnoreCase))

   {

       ListOfFiles.Items.Add(fileName);

 

       if (Replace)

       {

          // Write the modification into the same file

          StreamWriter streamWriter = File.CreateText(fileName);

 

streamWriter.Write(Regex.Replace(contents, txtFind.Text, txtReplace.Text, RegexOptions.IgnoreCase));

          streamWriter.Close();

       }

   }

}

 

 

After finding all the files and placing them in the array filenames, I loop through each file, load all the contents, check to see if there is a match and then make the replacement. I open the files as text so don’t use this with binary files or they could get corrupted. Also along these lines, using *.* for File Type is probably not a good idea!

Here’s the source: SearchAndReplace.zip

Posted in C#, Windows. 1 Comment »

InitialializeError error #2103 in control ‘Xaml1′: Invalid or malformed application: Check manifest

Got a new one today. I started working on a new Silverlight control and added it to an existing app when I got this error:

silverlight error 21031

I checked the manifest and double-checked the namespaces were correct etc. I finally fixed the problem by allowing anonymous access to the ClientBin folder. Here’s the code you need to include in the Web.Config in the configuration section:

<location path=ClientBin>

    <system.web>

        <authorization>

            <allow users=?/>

        </authorization>

    </system.web>

</location>

 

 Thanks to CatchX.com for this info!

Simple Password Regular Expressions

Here are a few regular expressions that can come in handy for passwords.

 

 Must contain at least 1 number, at least 1 lower case character, at least 1 upper case character and have a length of 8 or more:

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$

 

Must have a length of 8 or more, at least 1 upper case character, at least 1 number or special character (limited list of chars allowed):

^.*(?=.{8,})(?=.*[A-Z])(?=.*[@#$%^&+=1234567890]).*$