Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find in Files: Search all code in Team Foundation Server

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

Currently I perform a Get Latest on the entire codebase and use Windows Search, but this gets quite painful with over 1GB of code in 75,000 files.

EDIT: Tried the powertools mentioned, but the "Wildcard Search" option appears to only search filenames and not contents.

UPDATE: We have implemented a customised search option in an existing MOSS (Search Server) installation.

like image 658
Mark Glorie Avatar asked Sep 03 '08 01:09

Mark Glorie


People also ask

How do I search for TFS files?

Right-Click In the File List, right-click the file you want to check in and select Source Control > Check In (for selected files) or Source Control > Project > Check In All (for all files in the project).

How do I see all checked out files in TFS?

Right-click on the highest level that you want to search for checked out files (like the root of the project collection) and click the Find menu –> Find by Status. Recursive, and click Status. Click Find. This will give you a list of all the files that are checked out.

How do I search all codes in Visual Studio?

Visual Studio 17.2 Preview 3 introduces a brand-new All-In-One search experience that merges the existing VS Search (Ctrl + Q) and Go To (Ctrl + T) to allow you to search both your code and Visual Studio features quicker and easier than ever, all in the same place.


Video Answer


2 Answers

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

screenshot of code search filter syntax

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

like image 197
Grant Holliday Avatar answered Sep 22 '22 09:09

Grant Holliday


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;     } } } 
like image 28
4 revs, 4 users 97% Avatar answered Sep 23 '22 09:09

4 revs, 4 users 97%