扩展/工具在项目上执行清理

时间:2017-12-14 10:20:47

标签: visual-studio powershell

我最近一直在使用Visual Studio 2017,我希望有一个外部工具/扩展/设置,删除除.sln,.vcxproj和源之外的所有内容。我已经尝试过CLean Project和Clean Solution扩展,但这些都没有删除Debug文件夹。我已经阅读了有关PowerShell脚本的一些内容,但我不知道如何使用它们,我不想在我的控制台上运行未知代码。

PS:我知道VS有清理功能,但它只删除可执行文件。我还阅读了一些关于修改项目属性的内容,但对于许多项目来说这真的很不愉快。

PSS:我是学生,我有很多项目目录。我想要的只是一个整洁的方式存储它们。

PSSS:我已经配置了我的.gitignore文件,我正在使用git。有没有办法用它来执行清理?

2 个答案:

答案 0 :(得分:0)

我使用的脚本对我来说效果很好。

# 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."

复制并粘贴到.sln文件的同一目录中的新文件中。 我称之为" CleanAll.ps1"但你可以随意打电话。

运行它的最简单方法是什么?

右键单击文件>使用powershell运行

以递归方式从当前路径开始删除所有子文件夹的bin和obj文件。您当然可以个性化并添加" debug"或者您对脚本更熟悉后的任何其他文件夹。

答案 1 :(得分:0)

你正在使用Git。您可以简单地将工作区重置为上次提交,删除所有未版本控制和忽略的文件。首先确保没有挂起的更改,然后执行清理:

git clean -xdfn
  • -x:忽略忽略(删除binobj*.dll,...)
  • -d:删除文件以外的目录
  • -f:强制Git实际完成工作
  • -n:执行空运行,列出将要删除的文件。

从参数中删除n以实际清理工作区。

另见How do I clear my local working directory in git?