Possible Duplicate:
Ant task to check if a file exists?
How can one check if a file exist in a directory, if not use another file in different location. I tried the follow but does not help me with what I want. Could someone help?
<condition property="somefile.available">
<or>
<available file="/home/doc/somefile"/>
<and>
<available file="/usr/local/somefile" />
</and>
</or>
</condition>
If the name of the signature is passed, the file is checked for presence of that particular signature; otherwise the file is checked for the existence of any signature. It does not perform rigorous signature validation; it only looks for the presence of a signature. Since Apache Ant 1.7.
Nonexistence of one file results in false, although if neither exists they are considered equal in terms of content. This test does a byte for byte comparison, so test time scales with byte size. Note: if the files are different sizes, one of them is missing or the filenames match the answer is so obvious the detailed test is omitted.
This tutorial covers how to use the stat module in Ansible to check if files and folders exist on remote hosts. The easiest way to check if a file exists using Ansible is with the stat module. The purpose of the stat module is to retrieve facts about files and folders and record them in a register.
The full location of the Ant jar file. The home directory of Ant installation. The home directory for Ant library files - typically ANT_HOME/lib folder. The example presented in this chapter uses the ant.version built-in property. Er. Himanshu Vasishta
Use condition tests to establish if files are present. Then have a target for each true condition
<target name="go" depends="file-checks, do-something-with-first-file, do-something-with-second-file"/>
<target name="file-checks">
<available file="/home/doc/somefile" property="first.file.found"/>
<available file="/usr/local/somefile" property="second.file.found"/>
</target>
<target name="do-something-with-first-file" if="first.file.found">
???
</target>
<target name="do-something-with-second-file" if="second.file.found">
???
</target>
If you don't mind using ant-contrib you can take the procedural approach like this:
<if>
<available file="file1"/>
<then>
<property name="file.exists" value="true"/>
</then>
<else>
<if>
<available file="file2"/>
<then>
<copy file="file2" tofile="file1"/>
<property name="file.exists" value="true"/>
</then>
</if>
</else>
</if>
Otherwise you can probably use a target that is conditional on a property being set to indicate whether the file already exists in the preferred location, like Ant task to run an Ant target only if a file exists? but using unless
instead of if
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With