views:

626

answers:

3

I'd like to write a macro to crawl through the files in my project directory and find files that aren't included in the project.

In playing around with the DTE object, I see that the Project object has ProjectItems; if a ProjectItem represents a directory, then it has its own ProjectItems collection. This gives me all files that are included in the project.

So I could crawl recursively through each ProjectItems collection, and for each ProjectItem that's a directory, check to see if there are files in the file system that don't have a corresponding ProjectItem. This seems clumsy, though.

Any ideas of a simpler way to approach this?

+1  A: 

The approach I would take is to

  1. Enumerate the file system looking for all files
  2. Check and see if the given file has an associated project item.

Here is a quick bit of sample code

Function ContainsItem(p as Project, fileName as String) As Boolean
  Try
    Return p.ProjectItems.Item(fileName)
  Catch ex As ArgumentException
    Return False
  End Try
End Function

Function CotainsItem(dte as DTE, fileName as String) As Boolean
  For Each p As Project in dte.Solution.Projects
    Return ContainsItem(p, fileName)
  Next
End Function

Function GetFilesNotInProject(dte as DTE, startPath as String) as List(Of String)
  Dim list As New List(Of String)
  Dim files = Directory.GetFiles(startPath, "*.cs", SearchOPtions.AllDirectories)
  For Each file in files 
    If Not ContainsItem(dte, file) Then
      list.Add(file)
    End If
  Next
  Return list
End Function
JaredPar
Thanks - this helped a lot. It doesn't quite work as is, because `p.ProjectItems` only contains items at the root of the project; you have to recurse through each ProjectItem's own ProjectItems collection in turn. In the code I've posted, I build a list of existing files and then use that to see what's not included.
Herb Caudill
+1  A: 

Thanks to @JaredPar and @lpthnc for pointing me in the right direction. I ended up using an approach very similar to what @JaredPar outlines above. Here's my working macro FWIW.

Imports System.IO
Imports System.Collections.Generic
Imports EnvDTE

Public Module Main

    Sub IncludeNewFiles()
        Dim Count As Integer = 0
        For Each Project As Project In DTE.Solution.Projects
            If Project.UniqueName.EndsWith(".vbproj") Then
                Dim NewFiles As List(Of String) = GetFilesNotInProject(Project)
                For Each File In NewFiles
                    Project.ProjectItems.AddFromFile(File)
                Next
                Count += NewFiles.Count
            End If
        Next
        DTE.StatusBar.Text = String.Format("{0} new file{1} included in the project.", Count, If(Count = 1, "", "s"))
    End Sub

    Private Function GetAllProjectFiles(ByVal ProjectItems As ProjectItems, ByVal Extension As String) As List(Of String)
        GetAllProjectFiles = New List(Of String)
        For Each ProjectItem As ProjectItem In ProjectItems
            For i As Integer = 1 To ProjectItem.FileCount
                Dim FileName As String = ProjectItem.FileNames(i)
                If Path.GetExtension(fileName).ToLower = Extension Then
                    GetAllProjectFiles.Add(fileName)
                End If
            Next
            GetAllProjectFiles.AddRange(GetAllProjectFiles(ProjectItem.ProjectItems, Extension))
        Next
    End Function

    Private Function GetFilesNotInProject(ByVal Project As Project) As List(Of String)
        Dim StartPath As String = Path.GetDirectoryName(Project.FullName)
        Dim ProjectFiles As List(Of String) = GetAllProjectFiles(Project.ProjectItems, ".vb")
        GetFilesNotInProject = New List(Of String)
        For Each file In Directory.GetFiles(StartPath, "*.vb", SearchOption.AllDirectories)
            If Not ProjectFiles.Contains(file) Then GetFilesNotInProject.Add(file)
        Next
    End Function

End Module
Herb Caudill
A: 

Hi, this is just what I am looking for. I have added this as a macro using tools > macros > macro explorer and then added it as a toolbar button using tools > customize > macro. However I don't understand how to run it against a particular project? - when I open a project and click the custom button it just says '0 new files included in project'. I also need to allow for other new file types added (css/images/html/aspx/ashx etc) and not sure if I need to alter the script to handle this and if so which bit (i'm guessing this bit: Directory.GetFiles(StartPath, "*.vb", SearchOption.AllDirectories) ).

Thanks

Adam

Herb Caudill