我们有很大的解决方案(300多个项目),这是很长一段时间(7年)的发展,并且随着时间的推移,已经进行了大量的重构(项目被移除,移动,......)。我们注意到有时当某些项目从解决方案中删除时,它们不会从解决方案中删除,而且我们还会留下一些剩余的文件。
我们希望找到所有这些类型的文件,以便删除大部分文件(不是全部)。
有没有办法列出所有这些文件? (我们有Visual Studio 2015企业)
答案 0 :(得分:2)
从this case检查此PowerShell脚本,该脚本应该可以满足您的需求。它解析项目文件以获取包含的代码文件。然后它将该列表与磁盘上的实际文件进行比较。其余文件是未使用/过时的文件。
该脚本可以从磁盘中删除未使用的文件,也可以将它们作为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
}
}
此外,其他社区成员使用此脚本并在以下位置共享脚本:
@Marc Climent:我使用此脚本创建了一个更详细的脚本,其中包含其他类型的文件但不使用TFS:https://gist.github.com/mcliment/d9008a9288cea9d088af
@mikesigs:我也使用这个文件以及@ MarcCliment来创建另一个带有.sln文件而不是单个proj文件的PowerShell脚本。它会删除所提供解决方案中所有项目中排除的所有文件:https://gist.github.com/mikesigs/3512dbccc1767d447977#file-deleteexcludedfiles-ps1