- | rssFeed | My book on MSBuild and Team Build | Archives and Categories Wednesday, 03 May 2006

MSBuild fun part 1: Copying files

This is the beginning of a new series of blogs that I plan on writing. Its called ‘MSBuild Fun’, the idea is that I’ll discuss some examples of how you might use MSBuild, outside of building applications. MSBuild is a tool that certainly is geared toward building applications, but is not limited to that. Knowledge of MSBuild can help you with some small things that come up quite often.

In this example we will consider a relatively simple task, copying a set of files from one location to another. Let’s say that you would like to locate all image files contained in a particular location and copy those files to another. There are a few options; you can use the Windows Explorer to search for all of the image files, and copy-paste them to the other location. There are many problems associated with this approach:

Another option is to use the xcopy command from the command prompt. This is a good method to employ to solve the problems associated wit the previous approach. Using this approach we can continue on error,  and preserve the directory hierarchy.  We can also exclude files from being copied. If you are to perform this task many times that you can simply insert the call into a .bat file to preserve the command.
The approach we will use here is to use MSBuild
Copy task to copy the files from the source to the destination. It solves all the problems associated with the first approach, and throws in a few bonuses. Those are:

From a performance perspective the xcopy and MSBuild approach are pretty much equivalent, so that is not a factor. Now lets have a look at this in action.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="CopyFiles">

    <ItemGroup>

      <FilesToCopy Include="**\*.doc;**\*.txt"

                   Exclude="**\*trash*"/>

    </ItemGroup>

 

    <PropertyGroup>

      <Dest>Dest2</Dest>

    </PropertyGroup>

 

    <Target Name="CopyFiles">

      <Copy SourceFiles="@(FilesToCopy)"

            DestinationFiles="@(FilesToCopy->'$(Dest)\%(RecursiveDir)\%(Filename)%(Extension)')"

            ContinueOnError="true"/>

    </Target>

</Project>

This simple MSBuild project file will copy all Word Documents and text files that don’t contain trash in the filename to another location maintaining the directory hierarchy. As you can see here, in the FilesToCopy item declaration, I’m using the include attribute to include a list of files to be copied, and the exclude attribute to exclude a list of files from being copied. You can place complex wildcard expressions in these attributes to select only the files you are truly interested in.

 

 

Sayed Ibrahim Hashimi

msbuild | Visual Studio Wednesday, 03 May 2006 06:02:18 (GMT Daylight Time, UTC+01:00)  #     |  Tracked by:
http://e01c.net/_36v [Pingback]
http://e01c.net/_2_x [Pingback]
http://e01c.net/_2tz [Pingback]
http://e01c.net/_2n0 [Pingback]
http://e01c.net/_2g2 [Pingback]
http://e01c.net/_294 [Pingback]
http://e01c.net/_226 [Pingback]