Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy delete specific file from the list

Tags:

file

groovy

I try to delete files which i can find in folderPath. But I want delete only that, which have in name "Jenkins".
How to define in list to delete only that file.?

Example :
In C:\test\test have 3 files, want delete that which have Jenkins in name :

Files

import groovy.io.FileType

String folderPath = "C:\\test" + "\\" + "test"
def list = []
def dir = new File("$folderPath")
dir.eachFileRecurse (FileType.FILES) { file ->
  list << file
}
list.each {
println it.findAll() == "Jenkins" // Just files witch include in list "Jenkins" name
}

Thanks for tips !

like image 696
John Doe Avatar asked Sep 12 '25 03:09

John Doe


1 Answers

Here you go:

Use either of the below two:

import groovy.io.FileType

String folderPath = "C:/test/test"
new File(folderPath).eachFile (FileType.FILES) { file ->
//Delete file if file name contains Jenkins
   if (file.name.contains('Jenkins')) file.delete()
}

or

Below one uses FileNameFinder class

String folderPath = "C:/test/test"
def files = new FileNameFinder().getFileNames(folderPath, '**/*Jenkins*')
println files
files.each { new File(it).delete()}
like image 156
Rao Avatar answered Sep 14 '25 21:09

Rao