Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / TFS

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

4.50/5 (4 votes)
12 Dec 2014CPOL 45.1K   10  
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:

C#
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)