2

We have big solution(300+ projets), which is evolving since a long time(7 years), and over the time, there has been a lot of refactoring(projects removed, moved, ...). We did notice that sometimes when some projects are deleted from the solution, they are not deleted from the solution, and also that we have some leftover files.

We would like to find all those kind of files in order to delete most of them(not all).

Is there a way to list all those files? (We have Visual Studio 2015 enterprise)

J4N
  • 19,480
  • 39
  • 187
  • 340
  • Which version of TFS are you using? Didn't you check in your projects after refactoring? – Cece Dong - MSFT Mar 02 '16 at 05:22
  • TFS2013, we are going to move to TFS2015. I don't understand your remark. Over the time, we deleted some projects, and when doing it, it only delete the project from the SLN, not from the TFS. Also sometimes some people did exclude some files from the project without deleting them on TFS. So I would like to be able to list all those files that are not in my solution, but are still present on TFS. – J4N Mar 02 '16 at 08:45
  • TFS is a source-controlled server, all your modifications to your code should be checked in to TFS. Every time, you want to change code, you or your team should get latest code from TFS. – Cece Dong - MSFT Mar 02 '16 at 10:04

1 Answers1

2

Check this PowerShell script from this case, which should do what you are looking for. It parses the project file to get the included code files. Then it compares that list to the actual files on disk. The remaining files are your unused/obsolete files.

The script can either delete the unused files from disk or pend them as deletes in TFS.

<#
.SYNOPSIS
Find and process files in a project folder that are not included in the project. 


.DESCRIPTION
Find and process files in a project folder that are not included in the project. 
Options to delete the files or to add them as pending deletes for TFS. Use TF.exe to pend the deletes and start the check-in process for the files.
This is necessary when trying to delete files that are not currently included in a Visual Studio project.

.PARAMETER Project
The path/name for the project file. 

.PARAMETER VsVersion
The Visual Studio version (10, 11, 12). Used to locate the tf.exe file.  

.PARAMETER DeleteFromDisk
Just delete the files from disk. No interaction with any source control.

.PARAMETER TfsCheckin
After pending the deletes, open the check-in dialog.

#>

[CmdletBinding()]
param(
    [Parameter(Position=0, Mandatory=$true)]
    [string]$Project,  
    [Parameter(Mandatory=$false)]
    [ValidateRange(10,12)] 
    [int] $VsVersion = 12,
    [switch]$DeleteFromDisk,
    [switch]$TfsCheckin
)

$ErrorActionPreference = "Stop"
$tfPath = "${env:ProgramFiles(X86)}\Microsoft Visual Studio $VsVersion.0\Common7\IDE\TF.exe"

$projectPath = Split-Path $project


if($Project.EndsWith("csproj"))
{
    $fileType = "*.cs"
}
else
{
    $fileType = "*.vb"
}
$fileType


$projectFiles = Select-String -Path $project -Pattern '<compile'  | % { $_.Line -split '\t' } | `
     % {$_ -replace "(<Compile Include=|\s|/>|["">])", ""} | % { "{0}\{1}" -f $projectPath, $_ }
Write-Host "Project files:" $projectFiles.Count


$diskFiles = gci -Path $path -Recurse -Filter $fileType | % { $_.FullName}
Write-Host "Disk files:" $diskFiles.Count


$diff = (compare-object $diskFiles $projectFiles  -PassThru) 
Write-Host "Excluded Files:" $diff.Count

#create a text file for log purposes
$diffFilePath = Join-Path $projectPath "DiffFileList.txt" 
$diff | Out-File $diffFilePath  -Encoding UTF8
notepad $diffFilePath


#just remove the files from disk
if($DeleteFileOnly)
{
    $diff | % { Remove-Item -Path $_ -Force -Verbose}
}
else #TFS options
{
    #this will add the files as pending deletes in TFS (awaiting check-in)
    $diff | % {
        [Array]$arguments = @("delete", "`"$_`"")
        & "$tfPath" $arguments
    }

    if($Checkin)
    {
        #start the check-in process for the pending deletes
        [Array]$arguments = "checkin", "/recursive", "$projectPath"
        & $tfPath $arguments
    }
}

Also, other community members expend this script and share the scripts at:

@Marc Climent: I used this script to create a more detailed one that includes other types of files and does not use TFS: https://gist.github.com/mcliment/d9008a9288cea9d088af

@mikesigs: I too used this file as well as @MarcCliment's to create yet another PowerShell script that takes a .sln file instead of a single proj file. It deletes all files excluded from all projects in the provided solution: https://gist.github.com/mikesigs/3512dbccc1767d447977#file-deleteexcludedfiles-ps1

Community
  • 1
  • 1
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • I don't understand what it will compare? How TFS could know what is included in my visual studio solution(SLN/CSPROJ)? – J4N Mar 02 '16 at 08:44
  • But they will be on the two paths. Thoses files are present in the TFS(and locally), BUT they are not in the solution(not contained in any SLN/CSPROJ file) – J4N Mar 02 '16 at 10:39
  • Check the new screenshot. I compared the Source Path $/ScrumProject/TestCaseProject with Target Path C:\Users\xxx\xxx\Workspaces\ScrumProject\TestCaseProject (you can specify the path that you want to compare). You can see there is a .txt file in TFS, but not on local machine, and there is a .vspscc in the sub-folder on local, but not in TFS. – Cece Dong - MSFT Mar 03 '16 at 02:59
  • well, I don't know what you don't understand. The file is present locally, on the server, but not within the solution(SLN/CSPROJ). So there is no difference between the TFS repository and the local content. – J4N Mar 03 '16 at 07:08
  • Do you mean you want to destory the files that have been deleted in TFS? Delete means removing files and folders from the Team Foundation version control server and deletes them from the disk, which can be restore. Destroy means permanently delete version-controlled files from Team Foundation version control. The destroy action cannot be reversed. If you want to destory version-controlled files, you need to use the tf destroy command: https://msdn.microsoft.com/en-us/library/bb386005.aspx – Cece Dong - MSFT Mar 03 '16 at 07:37
  • No I mean find the files that are present locally(and in TFS) but not in the solution anymore. Do the following: Create a new solution, create a new project. save the whole solution. Delete the project. Now on the disk, we have the project and its files, but in the solution we don't have it anymore. I want to list all those files/folder – J4N Mar 03 '16 at 08:56
  • This is definitely the good direction. The only left issue that I've is that I've a solution with 300 projects, and there is also projects that are not in the solution(which should be listed as not used at all). – J4N Mar 04 '16 at 09:36
  • You can check the PowerShell Script that @mikesigs write which can find all excluded files in a Visual Studio Solution. – Cece Dong - MSFT Mar 07 '16 at 06:08