Home | Blog | Twitter @AndyJ | Contact Me | Snippets/Downloads | RSS

Ant: Moving files and directories

First off I wanna say that I'm running all my Ant task via Eclipse and not the command line. If there is a demand to know how to install Ant and run it from the command line I'll do that post separately.

So, what is Ant? Well to steal a quote from Mark Drew, think of Ant as .bat files on steroids. You use XML to describe a set of commands to run a whole range of tasks to do anything from SVN/CVS checkouts, unit tests, FTP, emails, sql, moving/copying folders/files generally just name it!

In this guide I want to cover creating a build.xml file and moving files/folders. I'm assuming you've got Eclipse and you know you way around it enough. Before we do begin you will need to ensure that you can see the Console View.

   view plainprintabout
 Window >> Show View >> Other >> General >> Console

Lets begin!

Create a new project in your Eclipse workspace. The type of project doesn't matter, though I'm creating a CFEclipse one as I want to move .cfm pages around. From within the project create a new file called Build.xml

   view plainprintabout
 right-click on the project folder >> new >> file >> file name (build.xml)
In your build.xml add the following and save:
   view plainprintabout
 <?xml version="1.0" encoding="UTF-8"?>
     <project name="myFirstBuildFile" default="" basedir=".">
         <description>
             A description of what this build file does
         </description>
         <!-- Simply echo's a message to the screen -->
         <echo message="Hello foo, I am the build.xml" />
     </project>

Now while still in the editor right-click anywhere on your build.xml file and choose the following:

   view plainprintabout
 Run As >> Ant Build
In the console window you should see something like
Buildfile: /[your eclipse work space root]/Ant/build.xml
[echo] Hello foo, I am the build.xml
BUILD SUCCESSFUL
Total time: 136 milliseconds

You've ran your first build.xml file excellent. Now lets move a file. Create two more folders in your Ant project called 'folder_test' and 'folder_live' and in the first folder put a file called 'index.cfm'. Your project should now look like this

  • build.xml
    • [folder_live]
    • [folder_test]
      • index.cfm
The idea of the next task to copy index.cfm from the test folder to the live one, i'm also going to introduce properties, think of them as variables.
   view plainprintabout
 <?xml version="1.0" encoding="UTF-8"?>
     <project name="myFirstBuildFile" default="" basedir=".">
         <description>
             A description of what this build file does
         </description>    
         <!-- Relative lo    cation of test folder -->
         <property name="test" value="folder_test/" />
         <!-- Relative location of live folder -->
         <property name="live" value="folder_live/" />
10      
11          <!-- move file -->
12          <move file="${test}index.cfm" todir="${live}" />    
13          <echo message="The index.cfm file has been moved" />
14      </project>
Now while still in the editor right-click anywhere on your build.xml file and choose the following:
   view plainprintabout
 Run As >> Ant Build
If all has gone well you should of just moved your index.cfm file from test to live. Thats all good and well but I doubt you ever have to move just one file my guess is that you have a host of folders and files. In the directory 'folder_test' add two other directories called 'images' and 'css' and move 'index.cfm' back as well. You new directory structure should look like this:
  • build.xml
    • [folder_live]
    • [folder_test]
      • [css]
      • [images]
      • index.cfm

The plan now is move the complete directory of 'folder_test' to 'folder_live':

   view plainprintabout
 <?xml version="1.0" encoding="UTF-8"?>
 <project name="myFirstBuildFile" default="" basedir=".">
     <description>
         A description of what this build file does
     </description>    
     <!-- Relative lo    cation of test folder -->
     <property name="test" value="folder_test/" />
     <!-- Relative location of live folder -->
     <property name="live" value="folder_live/" />
10  
11      <!--
12          move directories and files
13  
14          From Ant 1.6.3 you can write the following as:
15          <move file="${test}" tofile="${live}" />
16      -->

17      <move todir="${live}">
18          <fileset dir="${test}"/>
19      </move>            
20  
21      <echo message="The contest of folder_test have been moved" />
22  
23      <!--
24          You'll notice that when moving the directories Ant deletes the
25          original folder. Because of this we are going to use <mkdir>
26          to re-create the original folder
27      -->
    
28  
29   <mkdir dir="${test}" />
30      <echo message="We have recreated the the folder_test directory" />
31  
32  </project>
Again while in the editor right-click anywhere on your build.xml file and choose the following:
   view plainprintabout
 Run As >> Ant Build

Of course there is more you can do, rather than going through full set examples here or some of the other features:

Move a set of files to a new directory

   view plainprintabout
 <!-- move all .cfm files and leave the scribble file in test -->
 <move todir="${live}">
  <fileset dir="${test}">
  <include name="*.cfm"/>
  <exclude name="scribble.cfm"/>
  </fileset>
 </move>

Move a list of files to a new directory

   view plainprintabout
 <move todir="${live}">
  <filelist dir="${test}">
  <file name="index.cfm"/>
  <file name="fridays_joke.cfm"/>
  </filelist>
 </move>

Append ".bak" to the names of all files in a directory.

   view plainprintabout
 <!--- Notice the new parameter here, includeemptydirs -->
 <move todir="${live}" includeemptydirs="false">
  <fileset dir="${test}">
  <exclude name="**/*.bak"/>
  </fileset>
  <mapper type="glob" from="*" to="*.bak"/>
 </move>

More resources:

Related Blog Entries

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
jM's Gravatar Another great article! I've now written my first Ant task. With solid articles like this I might eventually "get" this eclipse/cfeclipse/ant stuff ;-)

Thanks!
# Posted By jM | 9/28/07 5:55 PM

BlogCFC / created by Raymond Camden / running version 5.9.5.003 / Contact AndyJarrett.com / Pet Rescue SOS