MStudley
MStudley

Reputation: 735

How can I fix C# comment headers

After using an automatic comment header application, I found that it deleted the last return before the code started. This means that all of my files in the solution now look like this:

//-----------------------------------------------------------------------------
//  File Name: Mapper.cs
//  Creation Date: 13/02/2013
//  Author: person
//-----------------------------------------------------------------------------using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

I have a couple of hundred of files I now need to fix and my source control went away (Was on an EC2 Micro that lapsed). I seem to have encountered a series of unfortunate events here. Is there a macro, or something similar, that can help me get out of this bind?

Upvotes: 1

Views: 108

Answers (3)

Patrick Hofman
Patrick Hofman

Reputation: 157018

In Visual Studio you can do a replace all with wild cards enabling you to insert the line break. I don't have the IDE available now but it should be something like this:

Search in all files in the project for this using wildcards:

---using

Replace this by:

---\nusing

Or as Kirk suggested, use regex.

Search for:

---.*

Replace by:

---\n\1

Upvotes: 3

Lukkha Coder
Lukkha Coder

Reputation: 4460

  1. Click on Edit > Find and Replace > Find in Files.
  2. In the find and replace dialog box, expand the Find options group box and enable Use Regular Expressions.
  3. In the Find what: drop down paste -using System;
  4. In the Replace with: drop down paste -\rusing System;
  5. In the Look in: drop down use Entire Solution.
  6. Optional: You can restrict your search further by adding a *.cs to the Look at these file types: drop down.

Try your search with the Find Next and Replace options. If things look good, then pray to the Visual Studio Gods and click the Replace All button.

Upvotes: 1

Steve
Steve

Reputation: 216303

A different approach from using the IDE, you could start your LinqPAD and then run this code (well a backup before is not to forget)

void Main()
{
    string yourRootSolutionPath = @"D:\temp";
    foreach(string s in Directory.EnumerateFiles(yourRootSolutionPath, "*.cs", SearchOption.AllDirectories))
    {
        string[] lines = File.ReadAllLines(s);
        for(int x = 0; x < lines.Length; x++)
        {
            int pos = lines[x].IndexOf("---using");
            if(pos > 0)
            {
                lines[x] = lines[x].Substring(0, pos+3) + Environment.NewLine + lines[x].Substring(pos+3);
                File.WriteAllLines(s, lines);
                break;
            }
        }
    }
}

Upvotes: 1

Related Questions