2012-01-10 14 views
12

私は大きなソリューションを持っており、実際には私のソリューション(csprojファイルには含まれていません)に属していない多くの* .csファイルがあります。それらのすべてを見つけて削除する方法はありますか?ソリューションで使用されていないcsファイルを削除する

+1

ファイルはディスク上にありますが、プロジェクトには含まれておらず、ディスクから削除したいとお考えですか?それとも、あなたのソリューションには含まれていますが、クラスは使用されていませんか? – Ray

+1

未使用のクラスはResharperを使って見つけることができます:http://stackoverflow.com/questions/4646174/resharper-find-all-unused-classesしかし、あなたが求めているのかどうかは分かりません。 – Ray

+2

[Visual Studioマクロ:プロジェクトに含まれていないファイルを探すか?](http://stackoverflow.com/questions/2000197/visual-studio-macro-find-files-that-arent-included- in-the-project) – Ray

答えて

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 –

+3

私もこのファイルと@ MarcClimentを使用して、単一のprojファイルではなく.slnファイルを取るもう1つのPowerShellスクリプトを作成しました。すべてのファイルを削除します提供されたソリューションのプロジェクトをご覧ください。http:// g oo.gl/PdR9Fg – mikesigs

+0

@mikesigsの修正されたスクリプトを使用します。これは、魅力的ですが、一部のファイルがWPFアプリケーション(XAMLファイル)から非アクティブであることを間違って示しています。 – Roemer

6

ソリューションエクスプローラでプロジェクトを選択するときは、ソリューションエクスプローラのツールバーの[すべてのファイルを表示]ボタンをクリックします。これにより、プロジェクトディレクトリにファイルとフォルダが表示されますが、プロジェクトには含まれません。これにより、プロジェクトを削除したり、プロジェクトに読み込んだりすることができます。

私は自動化されたソリューションがわからないので、手動でプロジェクトごとに行う必要があります。

+1

はい、私はその解決策を知っていますが、私のプロジェクトはとても大変ですので、私はこれを自動化する何かを探しています:-( – Nagg

2

すべてのファイルをソース管理に追加します。プロジェクトの一部であるファイルのみが追加されるので、プロジェクト以外のファイルは追加されません。単純にすべてのファイルをコミットして、プロジェクトを他の場所にチェックアウトすることができます。関連するファイルのみがターゲットの場所でチェックアウトされます。

大規模なプロジェクトがあるとすれば、何らかの種類のソース管理がないと思われるので、既存の接続を切断し、新しいソースへのチェックアウト後に元のソースの場所をクリアする必要があります元の場所にコピーし、オリジナルのscmが元のファイルの削除を検出して削除を実行させるようにします。

関連する問題