Click here to Skip to main content
15,889,116 members
Articles / DevOps / TFS
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
12 Dec 2014CPOL 43.6K   10   3   17
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)


Written By
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.

Comments and Discussions

 
BugNot all change types are listed? Pin
Bunny Charles19-May-15 22:16
professionalBunny Charles19-May-15 22:16 
SuggestionCredentials authentication Pin
Bunny Charles19-May-15 22:13
professionalBunny Charles19-May-15 22:13 
Questionpowershell Pin
kiquenet.com4-May-15 20:23
professionalkiquenet.com4-May-15 20:23 
QuestionGetting error "There Is no working folder mapping for tfs..." Pin
Nimit Singh Thakur2-Apr-15 19:22
Nimit Singh Thakur2-Apr-15 19:22 
QuestionChange sheet #(you may go with change sheet number between 1-24000+ of a date) Pin
Nimit Singh Thakur1-Apr-15 0:08
Nimit Singh Thakur1-Apr-15 0:08 
AnswerRe: Change sheet #(you may go with change sheet number between 1-24000+ of a date) Pin
Harsha Bopuri2-Apr-15 6:11
Harsha Bopuri2-Apr-15 6:11 
GeneralRe: Change sheet #(you may go with change sheet number between 1-24000+ of a date) Pin
Nimit Singh Thakur2-Apr-15 19:19
Nimit Singh Thakur2-Apr-15 19:19 
GeneralRe: Change sheet #(you may go with change sheet number between 1-24000+ of a date) Pin
Bunny Charles19-May-15 22:15
professionalBunny Charles19-May-15 22:15 
QuestionC# static class Path Pin
Tableeker2-Mar-15 7:27
Tableeker2-Mar-15 7:27 
QuestionArguments Pin
John B Oliver7-Jan-15 10:03
John B Oliver7-Jan-15 10:03 
AnswerRe: Arguments Pin
Harsha Bopuri7-Jan-15 10:26
Harsha Bopuri7-Jan-15 10:26 
GeneralRe: Arguments Pin
John B Oliver12-Jan-15 10:48
John B Oliver12-Jan-15 10:48 
QuestionWhy? Pin
Tony4683516-Dec-14 7:56
Tony4683516-Dec-14 7:56 
AnswerRe: Why? Pin
Harsha Bopuri16-Dec-14 10:58
Harsha Bopuri16-Dec-14 10:58 
GeneralRe: Why? Pin
Tony4683516-Dec-14 12:12
Tony4683516-Dec-14 12:12 
QuestionDeployment? Pin
Joel Palmer16-Dec-14 4:45
Joel Palmer16-Dec-14 4:45 
AnswerRe: Deployment? Pin
Harsha Bopuri16-Dec-14 6:12
Harsha Bopuri16-Dec-14 6:12 
it works for cloud version of TFS (VisualStudioOnline.com) as well. You can run this from any environment where you are connected to your TFS. In my case it was on my local and my VS is connected to my team projects

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

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