2012-03-21 81 views
4

我需要刪除已從Visual Studio 2010解決方案中刪除但尚未從文件系統中刪除的文件(主要是.cs和.cshtml)。我知道如果選擇「顯示所有文件」選項,將顯示這些文件,但是,我不想手動搜索它們,因爲它會花費很多時間並且容易出錯。VS2010 - 刪除不在解決方案中的文件

有什麼辦法可以列出這些文件嗎?我正在使用Visual Studio 2010和Resharper 6.1(也許Resharper有一些選項可以做到這一點)。

+0

難道它不適用於排除但未被刪除的文件的擴展名嗎? – jim31415 2012-03-21 15:49:32

+0

這些文件不包含在csproj文件中。你的意思是什麼?排除擴展? – empi 2012-03-21 15:59:27

+0

對不起,它似乎只是ASP.NET項目功能。 – jim31415 2012-03-21 17:07:08

回答

3

我寫了一個基於@ jovball的答案的PowerShell腳本。主要區別是我的接受.sln文件並刪除從該解決方案中的所有項目排除的所有文件。

以下是發佈此答案時的腳本版本。但請檢查here獲取最新版本。

<# 
.SYNOPSIS 
Find all files excluded from a Visual Studio solution with options to delete. 

.DESCRIPTION 
Finds all excluded files in all projects in the provided Visual Studio solution with options to delete the files. 

.PARAMETER Solution 
The path to the .sln file 

.PARAMETER VsVersion 
The Visual Studio version (10, 11, 12) (Used to locate the tf.exe file) 

.PARAMETER DeleteFromTfs 
Mark files as pending deletion in TFS 

.PARAMETER DeleteFromDisk 
Delete the files directly from the disk 

#> 

[CmdletBinding()] 
param(
    [Parameter(Position=0, Mandatory=$true)] 
    [string]$Solution, 
    [Parameter(Mandatory=$false)] 
    [ValidateRange(10,12)] 
    [int] $VsVersion = 12, 
    [switch]$DeleteFromDisk, 
    [switch]$DeleteFromTfs 
) 
$ErrorActionPreference = "Stop" 
$tfPath = "${env:ProgramFiles(X86)}\Microsoft Visual Studio $VsVersion.0\Common7\IDE\TF.exe" 
$solutionDir = Split-Path $Solution | % { (Resolve-Path $_).Path } 

$projects = Select-String -Path $Solution -Pattern 'Project.*"(?<file>.*\.csproj)".*' ` 
    | % { $_.Matches[0].Groups[1].Value } ` 
    | % { Join-Path $solutionDir $_ } 

$excluded = $projects | % { 
    $projectDir = Split-Path $_ 

    $projectFiles = Select-String -Path $_ -Pattern '<(Compile|None|Content|EmbeddedResource) Include="(.*)".*' ` 
     | % { $_.Matches[0].Groups[2].Value } ` 
     | % { Join-Path $projectDir $_ } 

    $diskFiles = Get-ChildItem -Path $projectDir -Recurse ` 
     | ? { !$_.PSIsContainer } ` 
     | % { $_.FullName } ` 
     | ? { $_ -notmatch "\\obj\\|\\bin\\|\\logs\\|\.user|\.*proj|App_Configuration\\|App_Data\\" } 

    (compare-object $diskFiles $projectFiles -PassThru) | Where { $_.SideIndicator -eq '<=' } 
} 

Write-Host "Found" $excluded.count "excluded files" 

if ($DeleteFromTfs) 
{ 
    Write-Host "Marking excluded files as deleted in TFS..." 
    $excluded | % { 
     [Array]$arguments = @("delete", "`"$_`"") 
     & "$tfPath" $arguments 
    } 
} 
elseif($DeleteFromDisk) 
{ 
    Write-Host "Deleting excluded files from disk..." 
    $excluded | % { Remove-Item -Path $_ -Force -Verbose} 
} 
else 
{ 
    Write-Host "Neither DeleteFromTfs or DeleteFromDisk was specified. Listing excluded files only..." 
    $excluded 
} 
+0

我明白你的觀點,但由於腳本的內容是我的答案,所以看起來鏈接是合適的。我會在這裏發佈當前版本的腳本,但如果我對其進行更改(很有可能)並忘記更新我的答案,它可能會過時。我會盡我所能確保鏈接始終保持正常。 – mikesigs 2015-01-16 04:43:49

相關問題