Remove unused cs-files in solution
Asked Answered
C

4

14

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?

Crural answered 10/1, 2012 at 9:29 Comment(4)
You mean the files are on disk, but not included in any projects and you want to delete them from disk? Or do you mean they're included in your solution, but the classes never used?Bystreet
Unused classes can be found using Resharper: #4646674 but I'm not sure if that's what you're asking.Bystreet
possible duplicate of Visual Studio macro: Find files that aren't included in the project?Bystreet
Also another approached is discussed here: #561543Bystreet
T
7

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.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
    }
}
Tomato answered 2/5, 2014 at 4:51 Comment(3)
Thanks! I used this script to create a more detailed one that includes other types of files and does not use TFS: gist.github.com/mcliment/d9008a9288cea9d088afAntagonize
I too used this file as well as @MarcCliment's to create yet another PowerShell script that takes a .sln file instead of a single proj file. It deletes all files excluded from all projects in the provided solution. Check it out! goo.gl/PdR9FgBoltonia
I use the modified script from @mikesigs, works like a charm except that it wrongly shows some files as inactive from a WPF application (the XAML files).Nguyen
T
7

When you select the project in solution explorer, click on the button "Show all files" in the solution explorer's toolbar. This will show you files and folders in the project directory, but which are not included in the project. This allows you to delete them or readd them to the project.

I don't know of an automated solution, so you'd have to do this for each project manually.

Townsman answered 10/1, 2012 at 9:36 Comment(1)
Yes, I know that solution, but my project is very very BIG, so I am looking for something to automate this :-(Crural
T
7

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.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
    }
}
Tomato answered 2/5, 2014 at 4:51 Comment(3)
Thanks! I used this script to create a more detailed one that includes other types of files and does not use TFS: gist.github.com/mcliment/d9008a9288cea9d088afAntagonize
I too used this file as well as @MarcCliment's to create yet another PowerShell script that takes a .sln file instead of a single proj file. It deletes all files excluded from all projects in the provided solution. Check it out! goo.gl/PdR9FgBoltonia
I use the modified script from @mikesigs, works like a charm except that it wrongly shows some files as inactive from a WPF application (the XAML files).Nguyen
E
2

Use visual studio to add all files to source control. It will only add files that are part of a project so the non-project files will not be added. You can then simply commit all files and check the project out somewhere else. Only the relevant files would be checked out in the target location.

Given that you have a large project it is of course unlikely that you haven't already got some kind of source control, so you may have to break the existing connection, clear the original source location after the checkout to the new location, copy the target to original and let the originals scm detect the file removal in the original and submit the removal.

Edy answered 2/5, 2014 at 4:58 Comment(0)
D
-1

first you have to stop debugging (shift+F5) . after that you can right click on that *cs class and delete it simply .

Defoliate answered 29/9, 2021 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.