I'm trying to put in my groovy code find files in a folder with ".bak" file type and find coincidences with the name. this is my code but is dont working for me, is there a different way to do it?
import groovy.io.*
def listfiles(dir) {
Folder = "SomeFolder"
nameFile = "backup_2022"
dlist = []
flist = []
new File(dir).eachDir {dlist << it.name }
if(it.name.endsWith('.bak')) {
dlist.sort()
new File(dir).eachFile(FileType.FILES, {flist << it.name })
flist.sort()
return (dlist << flist).flatten()
}
}
html_to_be_rendered = """<select id="config" name="config">"""
fs = listfiles("\\\\SERVER\\BACKUPS\\${Folder}")
fs.each { it ->
html_to_be_rendered = """
${html_to_be_rendered}
<option value="${it}">${it}</option>
"""
}
return "${html_to_be_rendered}</select>"
I tried with if (it.name.endsWith('.bak'))
but it doesn't work.
CodePudding user response:
Check the following.
@NonCPS
def listFiles(def path) {
def filterBakFiles = ~/.*\.bak$/
new File(path).traverse(type: groovy.io.FileType.FILES, nameFilter: filterBakFiles) { file ->
println file
}
}