删除解决方案中未使用的 cs 文件 [英] Remove unused cs-files in solution

查看:29
本文介绍了删除解决方案中未使用的 cs 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

这个 PowerShell 脚本应该可以满足您的需求.它解析项目文件以获取包含的代码文件.然后将该列表与磁盘上的实际文件进行比较.其余文件是您未使用/过时的文件.

该脚本可以从磁盘中删除未使用的文件,也可以将它们作为 TFS 中的删除挂起.

<预><代码><#.概要查找并处理项目文件夹中未包含在项目中的文件..描述查找并处理项目文件夹中未包含在项目中的文件.删除文件或将它们添加为 TFS 的待删除文件的选项.使用 TF.exe 挂起删除并启动文件的签入过程.尝试删除当前未包含在 Visual Studio 项目中的文件时,这是必要的..PARAMETER 项目项目文件的路径/名称..PARAMETER 与版本Visual Studio 版本(10、11、12).用于定位 tf.exe 文件..PARAMETER 从磁盘删除只需从磁盘中删除文件.不与任何源代码控制交互..PARAMETER TfsCheckin待删除后,打开签入对话框.#>[CmdletBinding()]参数([参数(位置=0,强制=$true)][字符串]$项目,[参数(强制=$false)][验证范围(10,12)][int] $VsVersion = 12,[开关]$DeleteFromDisk,[开关]$TfsCheckin)$ErrorActionPreference = "停止"$tfPath = "${env:ProgramFiles(X86)}Microsoft Visual Studio $VsVersion.0Common7IDETF.exe"$projectPath = 分割路径 $projectif($Project.EndsWith("csproj")){$fileType = "*.cs"}别的{$fileType = "*.vb"}$文件类型$projectFiles = Select-String -Path $project -Pattern '<compile' |% { $_.Line -split ' ' } |`% {$_ -replace "(<编译包含=|s|/>|["">])", ""} |% { "{0}{1}" -f $projectPath, $_ }写主机项目文件:"$projectFiles.Count$diskFiles = gci -Path $path -Recurse -Filter $fileType |% { $_.全名}写主机磁盘文件:"$diskFiles.Count$diff = (compare-object $diskFiles $projectFiles -PassThru)写主机排除的文件:"$diff.Count#创建一个用于日志目的的文本文件$diffFilePath = Join-Path $projectPath "DiffFileList.txt"$差异|外文件 $diffFilePath -Encoding UTF8记事本 $diffFilePath#只是从磁盘中删除文件如果($DeleteFileOnly){$差异|% { Remove-Item -Path $_ -Force -Verbose}}其他 #TFS 选项{#这会将文件添加为 TFS 中的待删除文件(等待签入)$差异|%{[数组]$arguments = @("删除", "`"$_`"")&"$tfPath" $arguments}如果($签入){#启动挂起删除的签入过程[Array]$arguments = "checkin", "/recursive", "$projectPath"&$tfPath $arguments}}

I have a big solution and there are many *.cs-files that actually don't belong to my solution (not included to csproj-files) any more. Is there any way to find all of them and remove?

解决方案

This PowerShell script 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.0Common7IDETF.exe"

$projectPath = Split-Path $project


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


$projectFiles = Select-String -Path $project -Pattern '<compile'  | % { $_.Line -split '	' } | `
     % {$_ -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
    }
}

这篇关于删除解决方案中未使用的 cs 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆