Tuesday, August 22, 2017

How to clean up BIN and OBJ folders in a Visual Studio solution

Summary: Some ideas on cleaning up intermediate and output folders in Visual Studio projects.
There may be better (and more elegant) ways of cleaning up the output (BIN) and intermediate (OBJ) folders generated by Visual Studio build process, but the following script is probably the easiest option you can use:

@echo off
rem Delete BIN and OBJ folders from the immediate folder and all subfolders.
rem

rem Switch to the script folder.
cd "%~dp0"

rem Use the following to suppress the 'File Not Found' message if no folders are found.
setlocal enabledelayedexpansion 
for /f "tokens=*" %%G in ('dir /B /AD /S bin 2^>nul') do rmdir /S /Q "%%G"
for /f "tokens=*" %%G in ('dir /B /AD /S obj 2^>nul') do rmdir /S /Q "%%G"

Notice that the script call setlocal enabledelayedexpansion to allow the 2^>nul redirection in the for loops (without it, it would output the "File Not Found" message if the folder and subfolders do not hold the "BIN" or "OBJ" folders.

See also:
How to clean Visual Studio bin and obj folders
I want to delete all bin and obj folders to force all projects to rebuild everything
Want to suppress File not found output