Check if directory exists in Phing and prompt to continue?

Wyck picture Wyck · Aug 20, 2012 · Viewed 8.7k times · Source

I'm trying to check if a directory or file exists in Phing but I cannot even get the basics to work.

For example:

<project name="test" default="help" basedir="./">

<target name="clean" description="Deletes directory">

    <available file="/testy" type="dir" property="dir.Exists" />

        <if>
            <isset property="dir.Exists"/>
                <then>
                    <echo>Yep</echo>
                </then> 
        </if>

</target>

<phingcall target="clean" />

</project>

I get a weird error:

Error reading project file [wrapped: \build.xml:22:18: Error initializing nested  
element <echo> [wrapped: phing.tasks.system.IfTask doesn't support the 'echo' 
creator/adder.]]

Ultimately I wanted to add a conditional yes/no to proceed if a directory exists.

ps. The error has nothing to do with "nested element echo" as far as I can tell because if I remove the echo it still sends the same error, in fact I think this is a default syntax related error message or something.

Answer

dkinzer picture dkinzer · Aug 23, 2012

If all you need to do is delete the directory then just call delete on it. Phing will automatically check if it exists for you so you don't need to do the check:

<target name="clean">
    <delete
      dir="${project.basedir}/${source.directory}" quiet='true'
    />
</target>

The important delete attributes here are: Phing Delete Attributes

The following works fine in my system:

<target name='test'>
  <if>
    <available file='results' type='dir' />
    <then>
      <echo>Yep</echo>
    </then>
  </if>
</target>

So I'm thinking the way you are using the AvailableTask is incorrect.