2

What I am trying to do, is to find a file with NAnt. This file could by anywhere in a directory structure of a given folder.

I tried to this with the NAnt-foreach task (this works) but I am not quite convinced of that:

<target name="find-file">
    <fail message="Property param.dir must be set" unless="${property::exists('param.dir')}" />
    <fail message="Property param.pattern must be set" unless="${property::exists('param.pattern')}" /> 

    <property name="return.file" value="" />
    <foreach item="File" property="iterator.file">
        <in>
            <items>
                <include name="${param.dir}\**\${param.pattern}" />
            </items>
        </in>
        <do>
            <property name="return.file" value="${iterator.file}" if="${string::get-length(return.file) == 0}" />
        </do>
    </foreach>
</target>

Is there anybody aware of a better approach? If not how can I accomplish to exit the foreach-loop after the first element is found?

1 Answer 1

4

This nantcontrib function will put the matching filenames into a delimited string..

If you know that only one matching file will exist then it may get you what you want. If there are several then you could use the nant substring function to just get the first match by taking the substring up to the first delimiter.

The following nant script:

<?xml version="1.0" encoding="utf-8"?>
<project default="find-file2">

    <property name="NantContrib.dir" value="C:\Program Files\nantcontrib-0.85\" readonly="true" />

    <target name="LoadNantContrib">
        <loadtasks assembly="${NantContrib.dir}bin\NAnt.Contrib.Tasks.dll" />
    </target>

    <target name="find-file2" depends="LoadNantContrib">
        <fileset id="find.set">
            <include name="${param.dir}\**\${param.pattern}" />
        </fileset>
        <property name="return.file" value="${fileset::to-string('find.set', ' | ')}" />
        <echo message="return.file=${return.file}"/>
        <echo message="Found ${fileset::get-file-count('find.set')} files"/>
    </target>
</project>

...and the following folder structure:

\---folderroot
    +---folder1
    |       dontfindme.txt
    |       findme.txt
    |       
    +---folder2
    |       dontfindme.txt
    |       
    \---folderempty

...works as expected. Searching for findme.txt finds one file. Searching for dontfindme.txt finds two files. Searching for *.txt finds three files.

Example call:

nant -D:param.dir=folderroot -D:param.pattern=findme.txt

Example output:

find-file2:

     [echo] return.file=C:\Documents and Settings\rbaker\My Documents\nantfindfile\folderroot\folder1\findme.txt
     [echo] Found 1 files

BUILD SUCCEEDED
3
  • The NAnt contrib function fileset::to-string won't work unless there is a way to dynamically create filesets at runtime (I do not know the files). My code will get the first file which was found, but my question is how to terminate the loop.
    – Dennis
    Nov 23, 2010 at 9:24
  • Updated with example. Works perfectly. You asked for a way to terminate the loop early: I've not seen one. You also asked for a better approach. It's debatable whether it's better or not - it's certainly a viable alternative.
    – robaker
    Nov 23, 2010 at 11:51
  • @Wernight In what way won't it work with more than one file match?
    – robaker
    Oct 28, 2011 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.