2012-01-10 52 views
12

我有一個很大的解決方案,並且有很多* .cs文件實際上不屬於我的解決方案(不包括在csproj文件中)。有什麼方法可以找到它們並刪除?刪除解決方案中未使用的cs文件

+1

你的意思是文件在磁盤上,但沒有包含在任何項目中,你想從磁盤上刪除它們?或者你的意思是他們包含在你的解決方案中,但是這些類從未使用過? – Ray 2012-01-10 09:32:22

+1

未使用的類可以使用Resharper找到:http://stackoverflow.com/questions/4646174/resharper-find-all-unused-classes但我不確定這是你問的問題。 – Ray 2012-01-10 09:33:35

+2

[Visual Studio宏:查找未包含在該項目中的文件?]的可能重複(http://stackoverflow.com/questions/2000197/visual-studio-macro-find-files-that-arent-included-在項目中) – Ray 2012-01-10 09:46:04

回答

5

這個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 
    } 
} 
+1

謝謝!我使用這個腳本來創建一個更詳細的腳本,其中包含其他類型的文件,但不使用TFS: https://gist.github.com/mcliment/d9008a9288cea9d088af – 2014-05-14 16:11:39

+3

我也使用這個文件以及@ MarcCliment's來創建另一個PowerShell腳本,它需要一個.sln文件而不是一個proj文件,它會刪除所有排除在外的文件項目在提供的解決方案。檢查它!http:// g oo.gl/PdR9Fg – mikesigs 2015-01-15 06:44:05

+0

我使用來自@mikesigs的修改過的腳本,其工作方式類似於魅力,只是它錯誤地將某些文件顯示爲從WPF應用程序(XAML文件)處於非活動狀態。 – Roemer 2016-10-03 06:40:01

6

當您在解決方案資源管理器中選擇項目時,單擊解決方案資源管理器工具欄上的「顯示所有文件」按鈕。這將顯示項目目錄中的文件和文件夾,但未包含在項目中。這允許您刪除它們或將它們讀取到項目中。

我不知道自動化解決方案,所以你必須爲每個項目手動執行此操作。

+1

是的,我知道這個解決方案,但我的項目非常非常大,所以我正在尋找一些東西來實現這個功能:-( – Nagg 2012-01-10 09:40:19

2

使用visual studio將所有文件添加到源代碼管理。它只會添加作爲項目一部分的文件,因此不會添加非項目文件。然後,您可以簡單地提交所有文件,並在其他地方檢查項目。只有相關文件將在目標位置檢出。

鑑於您有一個大型項目,當然不太可能您還沒有獲得某種源代碼控制,因此您可能不得不中斷現有連接,在結帳後將原始源位置清除爲新的位置,將目標複製到原始位置,並讓原件scm檢測原件中的文件移除並提交移除。

相關問題