我与多个项目一起工作,我想递归删除所有名为“bin”或“obj”的文件夹,这样我就可以确保所有项目都会重新构建所有内容(有时这是迫使Visual Studio忘记所有以前的构建的唯一方法)。

是否有一种快速的方法来实现这一点(例如使用.bat文件),而不必编写。net程序?


当前回答

为了达到这个目标,我总是在我的解决方案中添加一个新的目标。

<Target Name="clean_folders">
  <RemoveDir Directories=".\ProjectName\bin" />
  <RemoveDir Directories=".\ProjectName\obj" />
  <RemoveDir Directories="$(ProjectVarName)\bin" />
  <RemoveDir Directories="$(ProjectVarName)\obj" />
</Target>

您可以从命令行调用它

msbuild /t:clean_folders

这可以是您的批处理文件。

msbuild /t:clean_folders
PAUSE

其他回答

我讨厌obj文件乱扔源代码树。我通常设置项目,以便它们在源树外输出obj文件。对于我通常使用的c#项目

 <IntermediateOutputPath>..\..\obj\$(AssemblyName)\$(Configuration)\</IntermediateOutputPath>

用于c++项目

 IntermediateDirectory="..\..\obj\$(ProjectName)\$(ConfigurationName)"

我认为你可以右键点击你的解决方案/项目,然后点击“清洁”按钮。

据我所知,它是这样工作的。我现在没有VS.NET,所以不能测试它。

这是我用于递归删除所有BIN和OBJ文件夹的批处理文件。

创建一个空文件,命名为DeleteBinObjFolders.bat 复制粘贴下面的代码到DeleteBinObjFolders.bat 将DeleteBinObjFolders.bat文件移动到与解决方案(*.sln)文件相同的文件夹中。

@echo off
@echo Deleting all BIN and OBJ folders...
for /d /r . %%d in (bin,obj) do @if exist "%%d" rd /s/q "%%d"
@echo BIN and OBJ folders successfully deleted :) Close the window.
pause > nul

考虑到PS1文件在currentFolder(你需要删除bin和obj文件夹的文件夹)中

$currentPath = $MyInvocation.MyCommand.Path
$currentFolder = Split-Path $currentPath

Get-ChildItem $currentFolder -include bin,obj -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse }

实际上,你可以进一步使用PS的建议,在项目目录中创建一个vbs文件,就像这样:

Option Explicit
Dim oShell, appCmd
Set oShell  = CreateObject("WScript.Shell")
appCmd      = "powershell -noexit Get-ChildItem .\ -include bin,obj -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse -WhatIf }"
oShell.Run appCmd, 4, false

为了安全起见,我包含了-WhatIf参数,所以如果在第一次运行时对列表感到满意,可以删除它。