Ant COPY task, want to copy the directory itself, not just its contents!

Discussion in 'Java' started by dxd, Jun 23, 2005.

  1. dxd

    dxd Guest

    I'm trying to use the ant copy task to copy a directory plus all its
    contents and subdirectories to a new directory e.g.

    Source directory is d:\temp\dir1

    Target directory is c:\temp

    I want to execute the copy task on d:\temp\dir1 and end up with

    But what I get is the contents of d:\temp\dir1 going to c:\temp not the
    directory itself. Since there are many subdirectories in d:\temp I
    don't want to just copy d:\temp to c:\temp.

    I tried calling XCOPY from Ant but that behaves the same way.

    Any suggestions?


    dxd, Jun 23, 2005
    1. Advertisements

  2. <not sure>
    Make the target directory "c:\temp\dir1" or "c:\temp\dir1\" ?
    </not sure>

    I am pretty sure that ANT offers either
    - automatic directory creation, or..
    - the ability to make the directory specifically.

    As an aside, your use of file/directory naming appears 'suspicious'.
    1) If a drive is 'C' - I strongly recommend you refer to it
    as 'C', rether than 'c'. Remove possible errors before they are
    encountered in some possibly fragile code.
    2) A quick check of some of the ANT build.xml's on my
    system, (largely obtained from folks that know what
    they are doing), use the '/' consistently, and these
    build files work just fine on Windows. I suggest you
    use forward slashes.

    So 'c:\temp\dir1' -> 'C:/temp/dir1'

    Andrew Thompson, Jun 23, 2005
    1. Advertisements

  3. dxd

    dxd Guest

    Thanks Andrew,

    I guess that might work (I'm at home tonight I'll try it in the office
    tomorrow) but trouble is, I don't want to have to parse the source
    directory to extract the name of the last directory then append it onto
    the target directory. I'm not even sure if you could do this in Ant but
    even if you can (and of course I could embed Javascript to do it) it
    would look a real mess.

    In fact I simplified my real code in my post, so I'm not concerned
    about backslash versus forward slash, I do handle this OK in the

    It looks like it's just a limitation of both <copy> and xcopy, but a
    bit of a pain!

    dxd, Jun 23, 2005
  4. dxd

    dxd Guest

    This approach did work, but I ended up embedding javascipt (which I
    didn't want to do, because I didn't want any dependency on additional
    libraries) so that I could get the last name of the source folder and
    construct the name of the target folder. Not very elegant!

    dxd, Jun 28, 2005
  5. wrote:

    Please do not top post on a thread where bottom posting has been the norm.
    Did you try setting the context directory to be the parent directory of
    the directory you want to copy and then include only the desired
    directory via filters? That is, in high level language, tell ANT to
    recursively copy all files/directories named "dir1" from D:/temp to C:/temp?

    Raymond DeCampo, Jun 28, 2005
  6. dxd


    Jul 6, 2007
    Likes Received:
    A solution to deep copy with ant COPY and includes="**/*"

    Javascript? Really?! :trytofly: :wink:

    This is an olde post but I found it when looking for a quick answer so here's a solution that works for me to do the "deep copy". Similar solution works for delete etc.

    <target name="copySQLFiles">
    <mkdir dir="${}"/>
    <copy todir="${}">
    <fileset dir="${master.source.sql.dir}" includes="**/*.sql" />

    You can of course start with use constant dirs instead of my ${} vars, say "C:/svn/myproductrepo" or leave out the .sql to copy all.
    HeidelbergMan, Nov 13, 2010
  7. dxd


    Jan 12, 2011
    Likes Received:
    To copy directory with all its content just add a trailing slash, just like:
    <copy todir="c:/temp/">
    <fileset dir="c:/temp/dir1/"/>
    Diplomat, Jan 12, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.