Question

I have a MSBuild script to copy specific files from source to destination like given below. I dont want to give *.txt but give file names because I want only these files. Here the directory "E:\Test100\" is repeated for multiple times (I can put it in a property, but still the property will be repeated).

Is there a way to simplify this to refer to these files from the specific directory which is defined once?

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="CopyAllFiles">
    <ItemGroup Label="FilesToCopy">
      <File Include="E:\Test100\1.txt"/>
      <File Include="E:\Test100\2.txt"/>
      <File Include="E:\Test100\3.txt"/>
      <File Include="E:\Test100\4.txt"/>
      <File Include="E:\Test100\5.txt"/>
      <File Include="E:\Test100\6.txt"/>
      <File Include="E:\Test100\7.txt"/>
      <File Include="E:\Test100\8.txt"/>
    </ItemGroup>
    <Copy SourceFiles="@(File)"
          DestinationFolder="E:\TestCopyFiles\"
          Retries="10"
          RetryDelayMilliseconds="100">
    </Copy>   
  </Target>
</Project>

Note: MSBuild script and files are in different directories.

Was it helpful?

Solution

Have a look at documentation about msbuild batching/list ttransformation etc. What you want is the typical way msbuild can deal with lists - if you want to keep repetition to a minimum, you wouldn't even have to specify the .txt part:

<ItemGroup Label="FilesToCopy">
  <FileName Include="1"/>
  <FileName Include="2"/>
  <FileName Include="3"/>
  <FileName Include="4"/>
  <FileName Include="5"/>
  <FileName Include="6"/>
  <FileName Include="7"/>
  <FileName Include="8"/>
</ItemGroup>
<ItemGroup>
  <File Include="E:\Test100\%(FileName.Identity).txt"/>
</ItemGroup>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top