MSBuild Task Syntax for Deleting Files

I am trying to write an MSBuild task that removes the Obj and PDB directory from my bin folder on my build assembly scripts and seems to not be able to make it work correctly.

Does anyone have an example where they do this or the like, or a link to a simple example of deleting files and a directory with MSBuild?

+60
msbuild msbuild-task
Sep 25 '09 at 17:37
source share
8 answers

If you want to delete the entire directory, you will need RemoveDir :

<RemoveDir Directories="Path/To/Obj" /> 

And if you want to delete PDB files from bin, you will need the Delete task:

 <Delete Files="Path/To/Bin/MyApp.pdb" /> 

Note that you cannot use wildcards in the Delete task, so if you have multiple pdb files, you need to provide an ItemGroup as an argument.

+79
Sep 25 '09 at 17:49
source share

First you can delete the files in these directories, and then the directory itself with

 <Target Name="SomeTarget"> <ItemGroup> <FilesToDelete Include="Path\To\Obj\**\*"/> </ItemGroup> <Delete Files="@(FilesToDelete)" /> <RemoveDir Directories="Path\To\Obj\" /> </Target> 
+110
Sep 27 '09 at 1:17
source share

Posting for others who might be facing the same problem that I was facing.

The β€œDelete” task cannot delete readonly files that I needed to do, for example, when MSBuild receives the latest data from TFS, the files are marked as read-only. I used the EXEC command to delete readonly files:

 <ItemGroup> <FileToDelete Include="c:\temp\fileToDelete.txt"/> </ItemGroup> <Exec Command="del /F /Q &quot;@(FileToDelete)&quot;"/> 
+16
Dec 14 '11 at 17:04
source share

This code is so ugly that it should appear with a bag of air traffic. ;-) But it is fast because it does not create a list of files to delete, etc.

 <Target Name="DeleteBuildFolder"> <Exec Command="RmDir /S /Q &quot;$(BuildFolder)&quot;" /> <Exec Command="RmDir /S /Q &quot;$(BuildFolder)&quot;" /> <Exec Command="RmDir /S /Q &quot;$(BuildFolder)&quot;" /> <Exec Command="RmDir /S /Q &quot;$(BuildFolder)&quot;" /> <Exec Command="RmDir /S /Q &quot;$(BuildFolder)&quot;" /> <Exec Command="RmDir /S /Q &quot;$(BuildFolder)&quot;" /> <Exec Command="RmDir /S /Q &quot;$(BuildFolder)&quot;" /> </Target> 

How many RmDir commands are needed? It is enough for several RmDir commands to return "The system cannot find the specified file" instead of "The directory is not empty." On my machine, it seems I need another RmDir if $$ (BuildFolder) is open in Windows Explorer. An antivirus program can affect RmDir, as Subversion sometimes does, but I would rather have AV protection than (incorrectly) manage the exception list.

+4
Aug 31 2018-11-11T00:
source share

Alternatively, you can first remove the readonly property from the file and complete the msbuild uninstall task.

Same:

 <Target Name="DeleteFiles"> <Message Text="Delete File" Importance="high"/> <Attrib Files="$(FileToDelete)" ReadOnly="false" /> <Delete Files="$(FileToDelete)" /> </Target>` 
+3
Jul 17 '12 at 11:57
source share

Published answers will work as long as you have to deal with one directory. If you have subfolders, RemoveDir will fail with a Directory not empty error.

A slightly general approach also takes care of subfolders:

 <Target Name="CleanOutDir"> <ItemGroup> <FilesToClean Include="$(OutDir)\**\*.*" /> <!-- Bit of .Net to get all folders and subfolders --> <FoldersToClean Include="$([System.IO.Directory]::GetDirectories(&quot;$(OutDir)&quot;))" /> </ItemGroup> <Delete Files="@(FilesToClean)"/> <RemoveDir Directories="@(FoldersToClean)" /> </Target> 
+3
Jul 30 '15 at 17:11
source share

In Visual Studio 2013, this is added at the end of my .csproj file before the close tag </Project>

 <Target Name = "clean_folders" AfterTargets="Clean"> <Exec Command = "rd /S /Q obj" /> <Exec Command = "rd /S /Q bin" /> </Target> 

It didn’t work at first, but I noticed that Visual Studio (or R #, not sure) re-added DesignTimeResolveAssemblyReferencesInput.cache to the obj folder and also added the current \bin folder again (I have different assemblies in different subfolders under \bin ). It cleared everything else, including 25 other build configurations that I have from the imported .csproj files (yes, I know).

Be careful if you perform a batch overhaul of more than one configuration, as it simply wipes out all previous efforts for each rebuild, leaving you with only the last. Whups.

+2
Aug 03 '14 at 23:07
source share

Just add another wrinkle that I discovered. I am using Visual Studio 2015. Published answers that are removed using wildcards are still troublesome for me. I suspect that wildcards are evaluated before assembly, not after . This means that deletion will not happen if the files you want to delete are created during build. It also leads to wonderful behavior when the removal works every second time you build, which makes it all very enjoyable for testing.

I refuse wildcards. For what I do, I know the files that cause the problems, and I hardcode (if you can call it inside the project file) the actual file names.

+1
Mar 31 '16 at 19:26
source share



All Articles