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 :
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 !
1 Answer
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()} 1 