Remove from fileTree


I want to remove files from a directory if they are contained also in another one.

For example:

dir1 = {file1, file2}
dir2 = {file2}

I want to remove file2 from dir1.

I’m trying to get the 2 sets of files with fileTree, but I don’t know how to remove a fileTree from the other one.

Could you help me please?


def fileNames = fileTree(dir1).files.collect {
} as Set
def deleteTree = fileTree(dir2).matching {
   fileNames.each {
      include "**/$"
delete deleteTree

Continuing the discussion from Remove from fileTree:

I did this workaround

def files1 = [] fileTree(dir: path_to_tar + "shared_libs/").visit { FileVisitDetails file -> files1 << } def files2 = [] fileTree(dir: path_to_tar + "libs/").visit { FileVisitDetails file -> if (!files1.contains( { files2 << } } delete fileTree(dir: path_to_tar + "libs/", exclude: files2)

amd it works. Later i will try your solution that seems great.