views:

476

answers:

4

Background: We are using VS 2008 and are in the process of upgrading from TFS 2005 to 2008.

We have a solution that contains several projects and overall have hundreds of code files. We want to add the same text as a comment to all of these files (a copyright message). Does anyone know of a quick/easy/efficient way to do this? Also, is there a way to do this via TFS so we don't have check out and check in every file?

I found some code on CodeProject on creating a macro which does this, but you have to open each file individually and then run the macro on each one, which we were hoping to avoid.

Thanks.

+2  A: 

It should be trivial for you guys to write a simple program which iterates through all of the directories, and all of the code files (assuming *.cs or the like) and add text to the top.

It should take less than 20 minutes to build, test, and run that.

As far as TFS is concerned, just perform a checkout command from the top of the tree. It will check out every file in source control. When done, you can check in from the top as well.

Assuming C#, look at System.IO.Directory.GetDirectories()...

Chris Lively
A: 

I think that one approach (Thanks to The Pragmatic Programmer Chapter 3 - Text Manipulation) is to create a script that does that job for you, maybe Python, PowerShell, or Perl.

Quaky
A: 

If you have Cygwin installed, you can use the following Bash script:

# Process all .cpp and .h files under the directory tree at $PROJECTROOT
# To add other file types, add more "-o -name \*.ext" clauses.
# $COPYRIGHTFILE is the file containing your copyright message
COPYRIGHTLENGTH=$(wc -l $COPYRIGHTFILE)
for file in $(find $PROJECTROOT -name \*.cpp -o -name \*.h); do
    if diff <(head -n $COPYRIGHTLENGTH $file) $COPYRIGHTFILE; then
        (cat $COPYRIGHTFILE; cat $file) > /tmp/file
        mv /tmp/file $file
    fi
done

This finds every .cpp or .h file under the directory tree $PROJECTROOT, compares the beginning to the copyright message, and if it differs, prepends the copyright to the file via a temporary file.

DISCLAIMER: Untested, test first and use at your own risk.

Adam Rosenfield
A: 

[I]s there a way to do this via TFS so we don't have check out and check in every file?

It's a good idea to have this change be part of the files' change histories. So, you do want to check out & check in all files.

Kevin Pilch-Bisson's blog post "Formatting all C# files in a solution" is a good resource. You can modify that algorithm to make the edits.

Jay Bazuzi