Click here to Skip to main content
Click here to Skip to main content

TFS: Track All Changed Files in Source Control from a Date/Change Set

, 12 Dec 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Get a list of all files that are modified from a date or change set

Introduction

This tip helps you to track all changes made on a code base from a given date (change set) on TFS.

Background

There may be scenarios where a team of developers would want to track all the files they have added/modified from a date. My code snippet will help them to achieve this in a simple C# logic connecting to TFS.

Using the Code

Create a console application on Visual Studio.

You will have Program.cs by default. Now let's add TfsHelper.cs and put the below code into it:

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System;
using System.Collections.Generic;

namespace Maintenance
{
    public class TfsHelper
    {
        List<string> changedFiles = new List<string>();
        public void Init(params string[] args)
        {
            string localPath = args[0];
            string versionFromString = args[1];
            TfsTeamProjectCollection tfs = null;
            if (args.Length > 2)
                {
                tfs = new TfsTeamProjectCollection(new Uri(args[2]));
                 }
            else return;
            VersionControlServer vcs = tfs.GetService<versioncontrolserver>();
            try
            {
                var changeSetItems = vcs.QueryHistory(localPath, 
                                                      VersionSpec.ParseSingleSpec(
                                                                        versionFromString, 
                                                                        null),
                                                      0, RecursionType.Full, null, 
                                                      VersionSpec.ParseSingleSpec(
                                                                        versionFromString, 
                                                                        null), 
                                                                        null, Int32.MaxValue, true, false);
                foreach (Changeset item in changeSetItems)
                {
                    DateTime checkInDate = item.CreationDate;
                    string user = item.Committer;
                    foreach (Change changedItem in item.Changes)
                    {
                        string filename = changedItem.Item.ServerItem.Substring
                        (changedItem.Item.ServerItem.LastIndexOf('/') + 1);
// Your choice of filters. In this case I was not interested in the below files.
                        if (!filename.EndsWith(".dll")
                            && !filename.EndsWith(".pdb")
                            && !filename.EndsWith(".csproj")
                             && !filename.EndsWith(".pubxml")
                              && !filename.EndsWith(".sln")
                               && !filename.EndsWith(".config")
                               && !filename.EndsWith(".log")
                            && filename.IndexOf(".") > -1
                            && changedItem.ChangeType.Equals(ChangeType.Edit))
                        {
                            if (!Convert.ToBoolean(args[3]))
                            {
                                filename = string.Format("{0} - {1} - {2} by {3}",
                                    filename,
                                    checkInDate.ToString("dd-MM-yyyy"),
                                    changedItem.ChangeType.ToString(),
                                    user);
                            }
                            if (!changedFiles.Contains(filename))
                            {
                                if (Convert.ToBoolean(args[4]))
                                    changedFiles.Add(changedItem.Item.ServerItem);
                                else
                                    changedFiles.Add(filename);
                            }
                        }
                    }
                }
                foreach (string file in changedFiles)
                    Console.WriteLine(file);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            if (changedFiles != null && changedFiles.Count > 0)
                Console.WriteLine
                ("-----------------------------------------\nTotal File count: " + 
                changedFiles.Count);
            Console.WriteLine
            ("-----------------------------------------\nPress any key to close");
            Console.ReadKey();
        }
    }
}

Now from your Program.cs, call your TfsHelper as below:

 class Program
    {
        static void Main(string[] args)
        {
           TfsChangedFiles();
        }
        private static void TfsChangedFiles()
        {

       //Arguments more indetails
<code>     // args[0]</code> // local repository path
<code>     // args[1]</code> // Change sheet #(you may go with change sheet number between 1-24000+ of a date) 
<code>     // args[2]</code> // your remote TFS collections URL
<code>     // args[3]</code> // true or false - get list of concatenated "File Name+Date+change Type"
<code>     // args[4]</code> // true or false - absolute path of the file? true, else only file name

            TfsHelper TFS = new TfsHelper();
            TFS.Init(@"C:\TfsWorkSpace", "1",
                "https://mysite.visualstudio.com/DefaultCollection",
                "true", "false")
        }
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Harsha Bopuri
Architect
United States United States
I am a Microsoft Certified Solution Developer, working as a “Senior .NET Solutions Architect” and hold a Doctorate in Information Technology. With an overall experience of 9+ Years in related fields. I have a tremendous desire to exceed in whatever I undertake. I am an excellent team player - I like to listen, and am open to what everyone has to offer. I am confident; not shy of taking up responsibilities. I am sincere, and give myself to the job on hand. I have the advantage of possessing good communication skills, and this gives me the confidence to deal with people. I am very artistic and creative - and I have the capability to express this in my work.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionArguments PinmemberJohn B Oliver7-Jan-15 11:03 
AnswerRe: Arguments PinmemberHarsha Bopuri7-Jan-15 11:26 
GeneralRe: Arguments PinmemberJohn B Oliver12-Jan-15 11:48 
QuestionWhy? PinmemberTony4683516-Dec-14 8:56 
AnswerRe: Why? PinmemberHarsha Bopuri16-Dec-14 11:58 
GeneralRe: Why? PinmemberTony4683516-Dec-14 13:12 
QuestionDeployment? PinmemberJoel Palmer16-Dec-14 5:45 
AnswerRe: Deployment? PinmemberHarsha Bopuri16-Dec-14 7:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150123.1 | Last Updated 12 Dec 2014
Article Copyright 2014 by Harsha Bopuri
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid