Find in Files: Search all code in Team Foundation Server

后端 未结 12 2207
情深已故
情深已故 2020-11-28 22:37

Is there a way to search the latest version of every file in TFS for a specific string or regex? This is probably the only thing I miss from Visual Source Safe...

相关标签:
12条回答
  • 2020-11-28 22:38

    If you install TFS 2008 PowerTools you will get a "Find in Source Control" action in the Team Explorer right click menu.

    TFS2008 Power Tools

    0 讨论(0)
  • 2020-11-28 22:40

    In my case, writing a small utility in C# helped. Links that helped me - http://pascallaurin42.blogspot.com/2012/05/tfs-queries-searching-in-all-files-of.html

    How to list files of a team project using tfs api?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.VersionControl.Client;
    using Microsoft.TeamFoundation.Framework.Client;
    using System.IO;
    
    namespace TFSSearch
    {
    class Program
    {
        static string[] textPatterns = new[] { "void main(", "exception", "RegisterScript" };  //Text to search
        static string[] filePatterns = new[] { "*.cs", "*.xml", "*.config", "*.asp", "*.aspx", "*.js", "*.htm", "*.html", 
                                               "*.vb", "*.asax", "*.ashx", "*.asmx", "*.ascx", "*.master", "*.svc"}; //file extensions
    
        static void Main(string[] args)
        {
            try
            {
                var tfs = TfsTeamProjectCollectionFactory
                 .GetTeamProjectCollection(new Uri("http://{tfsserver}:8080/tfs/}")); // one some servers you also need to add collection path (if it not the default collection)
    
                tfs.EnsureAuthenticated();
    
                var versionControl = tfs.GetService<VersionControlServer>();
    
    
                StreamWriter outputFile = new StreamWriter(@"C:\Find.txt");
                var allProjs = versionControl.GetAllTeamProjects(true);
                foreach (var teamProj in allProjs)
                {
                    foreach (var filePattern in filePatterns)
                    {
                        var items = versionControl.GetItems(teamProj.ServerItem + "/" + filePattern, RecursionType.Full).Items
                                    .Where(i => !i.ServerItem.Contains("_ReSharper"));  //skipping resharper stuff
                        foreach (var item in items)
                        {
                            List<string> lines = SearchInFile(item);
                            if (lines.Count > 0)
                            {
                                outputFile.WriteLine("FILE:" + item.ServerItem);
                                outputFile.WriteLine(lines.Count.ToString() + " occurence(s) found.");
                                outputFile.WriteLine();
                            }
                            foreach (string line in lines)
                            {
                                outputFile.WriteLine(line);
                            }
                            if (lines.Count > 0)
                            {
                                outputFile.WriteLine();
                            }
                        }
                    }
                    outputFile.Flush();
                }
            }
            catch (Exception e)
            {
                string ex = e.Message;
                Console.WriteLine("!!EXCEPTION: " + e.Message);
                Console.WriteLine("Continuing... ");
            }
            Console.WriteLine("========");
            Console.Read();
        }
    
        // Define other methods and classes here
        private static List<string> SearchInFile(Item file)
        {
            var result = new List<string>();
    
            try
            {
                var stream = new StreamReader(file.DownloadFile(), Encoding.Default);
    
                var line = stream.ReadLine();
                var lineIndex = 0;
    
                while (!stream.EndOfStream)
                {
                    if (textPatterns.Any(p => line.IndexOf(p, StringComparison.OrdinalIgnoreCase) >= 0))
                        result.Add("=== Line " + lineIndex + ": " + line.Trim());
    
                    line = stream.ReadLine();
                    lineIndex++;
                }
            }
            catch (Exception e)
            {
                string ex = e.Message;
                Console.WriteLine("!!EXCEPTION: " + e.Message);
                Console.WriteLine("Continuing... ");
            }
    
            return result;
        }
    }
    }
    
    0 讨论(0)
  • 2020-11-28 22:42

    Team Foundation Server 2015 (on-premises) and Visual Studio Team Services (cloud version) include built-in support for searching across all your code and work items.

    You can do simple string searches like foo, boolean operations like foo OR bar or more complex language-specific things like class:WebRequest

    You can read more about it here: https://www.visualstudio.com/en-us/docs/search/overview

    0 讨论(0)
  • 2020-11-28 22:44

    Assuming you have Notepad++, an often-missed feature is 'Find in files', which is extremely fast and comes with filters, regular expressions, replace and all the N++ goodies.

    0 讨论(0)
  • 2020-11-28 22:52

    This search for a file link explains how to find a file. I did have to muck around with the advice to make it work.

    1. cd "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE"
    2. tf dir "$/*.sql" /recursive /server:http://mytfsserver:8080/tfs

    In the case of the cd command, I performed the cd command because I was looking for the tf.exe file. It was easier to just start from that directory verses adding the whole path. Now that I understand how to make this work, I'd use the absolute path in quotes.

    In case of the tf search, I started at the root of the server with $/ and I searched for all files that ended with sql i.e. *.sql. If you don't want to start at the root, then use "$/myproject/*.sql" instead.

    Oh! This does not solve the search in file part of the question but my Google search brought me here to find files among other links.

    0 讨论(0)
  • 2020-11-28 22:52

    This add-in claims to have the functionality that I believe you seek:

    Team Foundation Sidekicks

    0 讨论(0)
提交回复
热议问题