Apache Ant – Check if a file exists with wildcard

The <available> command can help you to check if a file or folder exists. Unfortunately it doesn’t support wildcard checking. Here is an workaround using the <pathconvert> such that we can check if a file or folder exists with wildcard.

<pathconvert property="filePath" setonempty="false" pathsep=" ">
  <path>
    <fileset dir="./" includes="a_file_name_with_wildcard*" />
  </path>
</pathconvert>
<condition property="fileExists">
  <resourcecount when="greater" count="0">
    <fileset file="${filePath}"/>
  </resourcecount>
</condition>
<if>
  <istrue value="${fileExists}"/>
  <then>
    <echo message="File exists"/>
  </then>
  <else>
    <echo message="File NOT exists"/>
  </else>
</if>

 

Please note that it only works if there is only one file matched the wildcard file name. This is because the <pathconvert> would returns more than one file path if there are more than one file. So it’s not a perfect solution but should be good enough for you to explore more from this example.

Done =)

Reference: StackOverflow – How to use wildcard in Ant’s Available command

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s