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

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

  1. 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
    c:\temp\dir1

    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?

    Thanks

    David
     
    , Jun 23, 2005
    #1
    1. Advertising

  2. On 23 Jun 2005 09:32:12 -0700, wrote:

    > 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
    > c:\temp\dir1


    <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'

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Jun 23, 2005
    #2
    1. Advertising

  3. 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
    script.

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

    David


    Andrew Thompson wrote:
    > On 23 Jun 2005 09:32:12 -0700, wrote:
    >
    > > 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
    > > c:\temp\dir1

    >
    > <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'
    >
    > HTH
    >
    > --
    > Andrew Thompson
    > http://www.PhySci.org/codes/ Web & IT Help
    > http://www.PhySci.org/ Open-source software suite
    > http://www.1point1C.org/ Science & Technology
    > http://www.LensEscapes.com/ Images that escape the mundane
     
    , Jun 23, 2005
    #3
  4. 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!

    David

    wrote:
    > 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
    > script.
    >
    > It looks like it's just a limitation of both <copy> and xcopy, but a
    > bit of a pain!
    >
    > David
    >
    >
    > Andrew Thompson wrote:
    > > On 23 Jun 2005 09:32:12 -0700, wrote:
    > >
    > > > 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
    > > > c:\temp\dir1

    > >
    > > <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'
    > >
    > > HTH
    > >
    > > --
    > > Andrew Thompson
    > > http://www.PhySci.org/codes/ Web & IT Help
    > > http://www.PhySci.org/ Open-source software suite
    > > http://www.1point1C.org/ Science & Technology
    > > http://www.LensEscapes.com/ Images that escape the mundane
     
    , Jun 28, 2005
    #4
  5. Re: Ant COPY task, want to copy the directory itself, not just itscontents!

    wrote:

    Please do not top post on a thread where bottom posting has been the norm.

    > 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!
    >
    > David
    >
    > wrote:
    >
    >>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
    >>script.
    >>
    >>It looks like it's just a limitation of both <copy> and xcopy, but a
    >>bit of a pain!
    >>
    >>David
    >>
    >>
    >>Andrew Thompson wrote:
    >>
    >>>On 23 Jun 2005 09:32:12 -0700, wrote:
    >>>
    >>>
    >>>>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
    >>>>c:\temp\dir1
    >>>
    >>><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'
    >>>


    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?

    Ray

    --
    XML is the programmer's duct tape.
     
    Raymond DeCampo, Jun 28, 2005
    #5
  6. HeidelbergMan

    Joined:
    Jul 6, 2007
    Messages:
    2
    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="${master.build.source.sql.dir}"/>
    <copy todir="${master.build.source.sql.dir}">
    <fileset dir="${master.source.sql.dir}" includes="**/*.sql" />
    </copy>
    </target>

    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
    #6
  7. Diplomat

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

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JL
    Replies:
    1
    Views:
    355
  2. Victor
    Replies:
    0
    Views:
    8,911
    Victor
    Sep 1, 2004
  3. john martin
    Replies:
    2
    Views:
    2,257
    john martin
    Apr 7, 2005
  4. krabhi
    Replies:
    1
    Views:
    9,462
    Marco Meschieri
    Aug 9, 2006
  5. teggy
    Replies:
    0
    Views:
    853
    teggy
    May 29, 2007
Loading...

Share This Page