2

when I copy some files using the following task

    copy: {
        tmp: {
            src: 'lib/public/**',
            dest: 'tmp/'
        }
    }

this is my source

|_lib
    |_public
            |_ dir1
            |_ dir2
            |_ index.html

My directory structure from the copy method looks like the following

|_tmp
     |_lib
          |_public
                  |_ dir1
                  |_ dir2
                  |_ index.html

I'd prefer if the directory looked more like this

|_tmp
     |_ dir1
     |_ dir2
     |_ index.html

In other words I want everything from lib/public to be copied to tmp is there another option I have to enable?

Subtubes
  • 15,851
  • 22
  • 70
  • 105

2 Answers2

6

Update: to copy the contents (folders and files) without the root source directory, you can use the cwd option. Looks like you've discovered this based on your comments.

copy: {
  tmp: {
    expand: true,
    cwd: 'lib/public',
    src: '**',
    dest: 'tmp/'
  }
}


You can achieve this using the flatten and filter properties. Since you want the files without parent directories, you'll want to specify the 'isFile' filter. To use flatten you'll need to enable the expand option. The documentation covers this and other options under the "Building the files object dynamically" section.

Your configuration should resemble the following:

copy: {
  tmp: {
    expand: true,
    flatten: true,
    src: 'lib/public/**',
    dest: 'tmp/',
    filter: 'isFile'
  }
}

Be aware that files that share the same name will be overwritten (the deeper nested file will overwrite the ones higher up in the tree).

Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
  • I edited my example to be more specific. the example you give is almost what I need but not quite. If you get a moment perhaps you can offer an alternative answer? – Subtubes Dec 30 '13 at 01:32
  • Actually the link you posted helped I found that what I really need to do is this tmp: { expand: true, cwd: 'lib/public', src: '**', dest: 'tmp/' } – Subtubes Dec 30 '13 at 01:39
  • 1
    @EdgarMartinez updated. Looks like you found the solution already! – Ahmad Mageed Dec 30 '13 at 01:57
0

You'll want the flatten option. You can read my answer to this other question (not exactly a duplicate), but it's fairly simple:

copy: {
    tmp: {
        src: 'lib/public/**',
        dest: 'tmp/',
        flatten: true,
        expand: true
    }
}
Community
  • 1
  • 1
Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
  • I edited my example to be more specific. The example you give is almost what I need but not quite. If you get a moment perhaps you can offer an alternative answer? What I really am trying to avoid is the copying of the parent folders and just what is inside them including the directories – Subtubes Dec 30 '13 at 01:33
  • 1
    Aaaah... I see. Well, I think @ahmad-mageed's answer is quite thorough with the update then. Glad you figured it out. – Jordan Kasper Dec 30 '13 at 02:59