0

I have been recently using Visual Studio 2017 and I'd like to have an external tool/extension/setting which deletes everything besides .sln, .vcxproj and sources. I have already tried CLean Project and Clean Solution extension but neither of those removes Debug folder. I have read something about PowerShell scripts but I have no idea how to use them and I don't want to run unknown code on my console.

PS: I know that VS has the cleanup function, but it only deletes executables. I also read something about modifying the project properties but that would be really unpleasant for many projects.

PSS: I am a student and I have many project directories. All I want is to have a neat way to store them.

PSSS: I have already configured my .gitignore file and I am using git. IS there a way to use it to perform cleanup?

jv42
  • 8,521
  • 5
  • 40
  • 64
CyberFox
  • 342
  • 3
  • 16

2 Answers2

0

Here a script I'm using that works well for me.

# PowerShell script that recursively deletes all 'bin' and 'obj' (or any other specified) folders inside current folder

$CurrentPath = (Get-Location -PSProvider FileSystem).ProviderPath

# recursively get all folders matching given includes, except ignored folders
$FoldersToRemove = Get-ChildItem .\ -include bin,obj -Recurse   | where {$_ -notmatch '_tools' -and $_ -notmatch '_build'} | foreach {$_.fullname}

# recursively get all folders matching given includes
$AllFolders = Get-ChildItem .\ -include bin,obj -Recurse | foreach {$_.fullname}

# subtract arrays to calculate ignored ones
$IgnoredFolders = $AllFolders | where {$FoldersToRemove -notcontains $_} 

# remove folders and print to output
if($FoldersToRemove -ne $null)
{           
    Write-Host 
    foreach ($item in $FoldersToRemove) 
    { 
        remove-item $item -Force -Recurse;
        Write-Host "Removed: ." -nonewline; 
        Write-Host $item.replace($CurrentPath, ""); 
    } 
}

# print ignored folders to output
if($IgnoredFolders -ne $null)
{
    Write-Host 
    foreach ($item in $IgnoredFolders) 
    { 
        Write-Host "Ignored: ." -nonewline; 
        Write-Host $item.replace($CurrentPath, ""); 
    } 

    Write-Host 
    Write-Host $IgnoredFolders.count "folders ignored" -foregroundcolor yellow
}

# print summary of the operation
Write-Host 
if($FoldersToRemove -ne $null)
{
    Write-Host $FoldersToRemove.count "folders removed" -foregroundcolor green
}
else {  Write-Host "No folders to remove" -foregroundcolor green }  

Write-Host 

# prevent closing the window immediately
$dummy = Read-Host "Completed, press enter to continue."

Copy and paste in a new file in the same directory of your .sln file. I call it "CleanAll.ps1" but you can call it as you prefer.

The easiest way to run it?

Right-Click on the file > Run with powershell

It recursively delete bin and obj files of all sub-folders starting from the current path. You can of course personalize and add "debug" or any other folder once you get more familiar with the script.

Daniele D.
  • 2,624
  • 3
  • 36
  • 42
  • Thank you for your suggestion! IIs it possible to this as an external tool in VS 2017 ? – CyberFox Dec 15 '17 at 13:33
  • I have tried to run it. I saved it in a text file and then right-clicked on it and clicked Open with PowerShell. A window popped up and vanished quickly. Nothing changed. – CyberFox Dec 15 '17 at 19:55
  • The last line should have avoided that # prevent closing the window immediately $dummy = Read-Host "Completed, press enter to continue." – Daniele D. Dec 17 '17 at 12:00
0

You're using Git. You can simply reset your workspace to the last commit, removing all unversioned and ignored files. First make sure you have no pending changes, then perform a clean:

git clean -xdfn
  • -x: ignore the ignores (removes bin, obj, *.dll, ...)
  • -d: remove directories in addition to files
  • -f: force Git to actually do the job
  • -n: perform a dry run, which will list the files that will be removed.

Remove the n from the arguments to actually clean the workspace.

See also How do I clear my local working directory in git?.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • This worked perfectly! Thank you very much, sir! Is there a way to pin this to VS? I had to use "cd" to go to the folder, but would there be a way to automate this? – CyberFox Dec 15 '17 at 13:34
  • https://stackoverflow.com/questions/213236/creating-visual-studio-toolbar-commands-to-execute-batch-files – CodeCaster Dec 15 '17 at 13:58
  • That is a really nice post. I am using the external tools. My only problem is that I have to "cd" to the project directory each time and then run the command. My issue is: How do I tell it to go to the project directory? I am really sorry, but I have never used a batch file, nor wrote one. – CyberFox Dec 15 '17 at 20:04