Skip to content

Cory Foy

Organizational agility through intersecting business and technology

Menu
  • FASTER Fridays
  • Mapping Mondays
  • Player Embed
  • Search Videos
  • User Dashboard
  • User Videos
  • Video Category
  • Video Form
  • Video Tag
Menu

MSBuild task to recursively delete all of a folder’s contents

Posted on March 14, 2007July 16, 2009 by Cory Foy

Following the Perl mantra of TMTOWTDI – we had a question come up about how to recursively delete a folder’s contents – without deleting the folder itself – using MSBuild.

There are several solutions, but if you have a lot of crud to delete, one of the faster ways is to create a target which has the following calls:

<exec command="dir /ad /b c:\Temp > files.txt"> files.txt"/> </exec><exec command='for /f "Tokens=*" %%i in (files.txt) do rd /s/q "C:\Temp\%%i"'> <delete files="files.txt"></delete></exec>

This spits out all of the subfolders to a temporary text file, then deletes them by looping over them and calling rd. The reason you have to do it this way is that if you just use the for loop without the Tokens=* line, it won’t find directories with spaces in the name, as it, by default, only reads in the first space delimited token.

The reason it has to be to calls is that the “dir /ad /b c:\Temp” call has to be in single-quotes in the for command, but the Tokens=* has to be in double quotes. Since one of those is already taken because we are in an XML attribute, you need to break it into two separate calls.

Another way would be to break the commands into two seperate properties, and then exec that:

<propertygroup>   <dir_list>dir /ad /b c:\Temp</dir_list>   <del_command>for /f "Tokens=*" %%i in ('$(dir_list)') do rd /s/q "C:\Temp\%%i"</del_command> </propertygroup>

<target name="DeleteTemp">      <exec command="$(del_command)"> </exec></target>

8 thoughts on “MSBuild task to recursively delete all of a folder’s contents”

  1. Anonymous says:
    October 5, 2007 at 10:37 am

    Cory what about

    &ltItemGroup;>
    &ltSettingsFiles; Include=”$(ProgramFiles)\App\Settings\**”/>
    </ItemGroup>

    <Target; Name=”DeleteAppSettingsFiles”>
    <Delete; Files=”@(SettingsFiles)” />
    </Target>

  2. FreeToDev says:
    April 1, 2008 at 4:51 pm

    or use the CleanFolder task in the Microsoft SDC Tasks Library: http://www.codeplex.com/sdctasks

  3. Anonymous says:
    April 5, 2008 at 4:40 am

    Using that ‘DeleteAppSettingsFiles’ as is currently has two problems. 1. it will not delete read only files, 2. It will not delete the folders, only files.

    The CleanFolder task provides an easy mechanism to do this.

  4. Ps0a says:
    November 5, 2008 at 8:06 pm

    Very usefull!
    tks

    I use that in my site , ok?

  5. Martin says:
    July 16, 2009 at 9:28 am

    This post has messed up formatting.

  6. Cory Foy says:
    July 16, 2009 at 3:44 pm

    Thanks! Fixed.

  7. DONNA WYNN says:
    March 24, 2010 at 8:57 pm

    hello great blog yea nice job It sounds like you’re creating problems yourself by trying to solve this issue instead of looking at why their is a problem in the first place.

  8. Jose Lamas says:
    August 18, 2011 at 10:23 pm

    Hi, found this useful!

    You can get it to work in a single exec task by using single quotes for the entire exec command and encoding single quotes inside, or viceversa. That is you can write it as follows:

    <Exec Command=’for /f “Tokens=*” %%i in (%27dir /ad /b “$(FolderToClean)”%27) do rd /s/q “$(FolderToClean)\%%i”‘ />

    [hope it reads well once published]

    Besides, if the folder also contains files, and you need to get rid of them, as it was my case, an entire CleanFolder target can be written this way:

    <Target Name=”CleanFolder” >
    <ItemGroup>
    <ToDelete Include=”$(FolderToClean)\*.*” />
    </ItemGroup>
    <Delete Files=”@(ToDelete)” />

    <Exec Command=’for /f “Tokens=*” %%i in (%27dir /ad /b “$(FolderToClean)”%27) do rd /s/q “$(FolderToClean)\%%i”‘ />
    </Target>

    Cheers,

Comments are closed.

© 2025 Cory Foy | Powered by Superbs Personal Blog theme