MSBuild查找包含名为xxxx的文件的所有目录

时间:2010-03-04 14:58:24

标签: msbuild

给定文件夹结构:

parentFolder
  - ChildFolder1
    - somefiletolookfor.txt
    - (other files and folder)
  - ChildFolder2
    - (other files and folder)
  - ChildFolder3
    - (other files and folder)
  - ChildFolder4
    - somefiletolookfor.txt
    - (other files and folder)
  - ChildFolder5
    - (other files and folder)

我想获取ChildFolder1和ChildFolder4的文件夹路径。对于每个文件夹路径,我需要执行复制任务。

1 个答案:

答案 0 :(得分:14)

这是一个可行的方法:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <AllFiles Include="ChildFolder*\**"/>
    <SpecificFiles Include="ChildFolder*\somefiletolookfor.txt" />
  </ItemGroup>

  <Target Name="Demo">

    <Message Text="AllFiles: @(AllFiles)"/>
    <Message Text="====================="/>
    <Message Text="SpecificFiles: @(SpecificFiles)"/>
    <Message Text="====================="/>
    <Message Text="Specific Dirs: @(SpecificFiles->'%(RootDir)%(Directory)')"/>
    <Message Text="====================="/>
  </Target>

</Project>

以下是我制作的示例文件的结果:

  AllFiles: ChildFolder1\other.txt;ChildFolder1\somefiletolookfor.txt;ChildFolder2\other.txt;ChildFolder3\other.txt;ChildFolder4\other.txt;C
  hildFolder4\somefiletolookfor.txt;ChildFolder5\other.txt
  =====================
  SpecificFiles: ChildFolder1\somefiletolookfor.txt;ChildFolder4\somefiletolookfor.txt
  =====================
  Specific Dirs: C:\Data\Ibrahim\Development\MSBuild\FindFolders\ChildFolder1\;C:\Data\Ibrahim\Development\MSBuild\FindFolders\ChildFolder4\
  =====================