【问题标题】:VS2010 - remove files that are not in solutionVS2010 - 删除不在解决方案中的文件
【发布时间】:2012-04-06 04:01:06
【问题描述】:

我需要删除已从 Visual Studio 2010 的解决方案中删除但尚未从文件系统中删除的文件(主要是 .cs 和 .cshtml)。我知道如果我选择“显示所有文件”选项会显示这些文件,但是,我不想手动搜索它们,因为这会花费很多时间并且容易出错。

有没有办法列出这些文件?我正在使用 Visual Studio 2010 和 Resharper 6.1(也许 Resharper 有一些选项可以做到这一点)。

【问题讨论】:

标签: visual-studio-2010 resharper


【解决方案1】:

我根据@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
}

【讨论】:

  • 我明白你的意思,但由于脚本的内容是我的答案,所以链接似乎是合适的。我将在此处发布脚本的当前版本,但如果我对其进行更改(很有可能)并且忘记更新我的答案,它可能会过时。我会尽我最大的努力确保链接保持不变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-26
  • 2011-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多