buildallfilesinadirectoryexceptone

Cancel
Save
Edit

Build All Files in a Directory Except One

In a project file you can use wildcards to include all the files in one directory or a nested set of directories as inputs for a build. However, there might be one file in the directory or one directory in the nested set of directories that you do not want to include as input for a build. You can explicitly exclude that file or directory from the list of inputs.

In this example, the "MyAppExclude" project contains items of different types, including graphics in a nested set of directories. You can include or exclude specific files or use wildcards to include or exclude multiple files.

Excluding a File or Directory from the Inputs for a Build

Items are the inputs to a build. The items that you want to include are declared either separately or as a group using the Include attribute, for example:

<CSFile Include="Form1.cs"/>

<CSFile Include="*.cs"/>

<JPGFile Include="Images\*\.jpg"/>

If you have used wildcards to include all the files in one directory or a nested set of directories as inputs for a build, there might be one file in the directory or one directory in the nested set of directories that you do not want to include. To do this, use the Exclude attribute. For example, to include all .cs or .vb files except Form2:

<CSFile Include="*.cs" Exclude="Form2.cs"/>
or
<VBFile Include="*.vb" Exclude="Form2.vb"/>

To recursively include all .jpg files in subdirectories of the directory Images except those in the directory Version2:

<JPGFile
Include="Images\*\.jpg"
Exclude = "Images\*\Version2\.jpg"/>

You must specify the path in the same way in the Include and Exclude attributes. If you use an absolute path to specify file locations in the Include attribute, you must also use an absolute path in the Exclude attribute; if you use a relative path in the Include attribute, you must also use a relative path in the Exclude attribute.

Using Conditions to Exclude a File or Directory from the Inputs for a Build

If there are items that you want to include, for example, in a Debug build but not a Release build, you can use the Condition attribute to specify the conditions under which to include the item. In the following example, the file Formula.vb is included only in Release builds.

<Compile
Include="Formula.vb"
Condition=" '$(Configuration)' == 'Release' " />

Building the Project

To build the project, navigate to the directory that contains the project file MyAppExclude and type:
msbuild myappexclude.proj

If the directory contains only one file that has a file extension that ends in "proj", you can type only msbuild because the project file is used automatically by MSBuild.

Project Files

Download Code - C#

Visual C# example

		    <Project MSBuildVersion="2.0" DefaultTargets="Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
		        [<PropertyGroup>]
		            <builtdir>built</builtdir>
		        [</PropertyGroup>]
	

		        [<ItemGroup>]
		            [<CSFile] Include="*.cs Exclude="Form2.cs"/>
		            <Reference Include="System.dll"/>
		            <Reference Include="System.Data.dll"/>
		            <Reference Include="System.Drawing.dll"/>
		            <Reference Include="System.Windows.Forms.dll"/>
		            <Reference Include="System.XML.dll"/>
		        [</ItemGroup>]
	

		        <Target Name="PreBuild">
		            <Exec Command="if not exist $(builtdir) md $(builtdir)"/>
		        </Target>
	

		        <Target Name="Compile" DependsOnTargets="PreBuild">
		            <Csc	Sources="@(CSFile)"
	
References="@(Reference)"
OutputAssembly="$(builtdir)\$(MSBuildProjectName).exe"
TargetType="exe" />
		        </Target>
		    </Project>