Groovy delete specific file from the list
10,618
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()}
Author by
John Doe
Updated on June 08, 2022Comments
-
John Doe almost 2 years
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 !
-
Hugues M. over 6 years
file.path.contains('Jenkins')
is dangerous, you should usefile.name.contains('Jenkins')
. Imagine iffolderPath = "C:/Jenkins/test"
, you would delete all files in your loop.