jonatzin
jonatzin

Reputation: 972

Gradle scripting: copy content of child folders and additional folder to new locations

I have the following folder structure (there is an arbitrary number of child folder and the names are not known). I only have the path to the parent folder available.

Parent
  | Child_folder_0
  | Child_folder_N

as well as a separate folder called contentFolder

I am trying to copy the each child folder (and it's content) into a different location as well as copy the content of contentFolder into each child folder's new location.

Thanks!

Upvotes: 0

Views: 201

Answers (1)

Crazyjavahacking
Crazyjavahacking

Reputation: 9677

The following code:

def parentFolder  = 'Parent'
def contentFolder = 'contentFolder'
def destDir       = 'destDir'

task copyChildFilesFromParent(type: Copy) {
    from(parentFolder) {
        include '**/*'
    }
    into destDir
}

task copyContentFilesIntoChildren() {
    (parentFolder as File).eachDir { childDir ->
        copy {
            from(contentFolder) {
                include '**/*'
            }
            into "$destDir/${childDir.name}"
        }
    }
}

task copyFiles(dependsOn: ['copyChildFilesFromParent', 'copyContentFilesIntoChildren'])

Will transform the following directory structure:

├── build.gradle
├── contentFolder
│   ├── content.txt
│   └── data.dat
└── Parent
    ├── Child_folder_0
    │   ├── 0.dat
    │   └── data.0
    ├── Child_folder_1
    │   ├── 1.dat
    │   └── data.1
    ├── Child_folder_2
    │   ├── 2.dat
    │   └── data.2
    └── Child_folder_N
        ├── data.N
        └── N.dat

into

├── destDir
│   ├── Child_folder_0
│   │   ├── 0.dat
│   │   ├── content.txt
│   │   ├── data.0
│   │   └── data.dat
│   ├── Child_folder_1
│   │   ├── 1.dat
│   │   ├── content.txt
│   │   ├── data.1
│   │   └── data.dat
│   ├── Child_folder_2
│   │   ├── 2.dat
│   │   ├── content.txt
│   │   ├── data.2
│   │   └── data.dat
│   └── Child_folder_N
│       ├── content.txt
│       ├── data.dat
│       ├── data.N
│       └── N.dat

Upvotes: 1

Related Questions