Just Code‎ > ‎

Nant - Find File By Pattern

posted Sep 29, 2015, 11:27 PM by Peter Henell   [ updated Sep 29, 2015, 11:27 PM ]
This is a script snippet that returns the path to the first file found in ${directory} that matches ${pattern}

<script language="C#" prefix="files" >
    <code>
    <![CDATA[
        [Function("find-file")]
        public static string FindFirstFileInFolder(string startDir, string pattern)
        {
            // Returns first filename that matches the pattern.
            var files = Directory.GetFiles(startDir, pattern, SearchOption.AllDirectories);
            return files.Length > 0 ? files[0] : null;
        }
    ]]>
          </code>
  </script>

This examples finds the package zip file in the target folder and copies it to a specific package archive directory. 
<target name="copyZipToPackageFolder">
    <property name="pattern" value="*.zip" />
    <property name="target.folder" value="${directory::get-current-directory()}\target" />
    <property name="package.zip.path" value="${files::find-file(target.folder, pattern)}" />

    <copy file="${package.zip.path}" tofile="${archive.package.path}" />
  </target>
Comments