gpt4 book ai didi

visual-studio-2010 - 删除解决方案中未使用的 cs 文件

转载 作者:可可西里 更新时间:2023-11-01 08:08:00 25 4
gpt4 key购买 nike

我有一个大的解决方案,但有许多 *.cs 文件实际上不再属于我的解决方案(不包含在 csproj 文件中)。有没有办法找到所有这些并删除?

最佳答案

此 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
}
}

关于visual-studio-2010 - 删除解决方案中未使用的 cs 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8800977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com