Verifying downloaded file with dynamically generated part in text of the name of file

Hi everyone, whether someone has a solution for verifying downloaded file that has the same text in name and format, but always different number that is generated dynamically in name, for example “example-12.pdf”. So 12 is generated dynamically. And it can be any number, but text “example” is always the same.

It doesn’t work if I put only “example.pdf” since it searches for exact name.

File downloadFolder = new File(‘C:\Users\Downloads\’)

List namesOfFiles = Arrays.asList(downloadFolder.list())

if (namesOfFiles.contains(‘example-12.pdf’)) {

Thanks in advance.

@elizabeth_g Were you able to find the solution? Even I am stuck with this.

import java.util.regex.Pattern

File downloadsFolder = new File('/Users/username/Downloads')
assert downloadsFolder != null

List fileNames = Arrays.asList(downloadsFolder.list())
println fileNames

Pattern pattern = Pattern.compile("example-\\d+\\.pdf")
List exampleFiles =
						.filter {fname -> pattern.matcher(fname).matches()}
if (exampleFiles.size() > 0) {
	println exampleFiles
	println("found ${exampleFiles.size()} files that match regex ${pattern}")

Thanks for your input. Having a question how do you verify if the folder really contains this file and how to make this step passed and failed if the folder doesn’t contain an expected file?

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.regex.Pattern

import com.kms.katalon.core.util.KeywordUtil

 * Scans the folder "/Users/name/Downloads" for files "example-xxx.pdf".
 * If found print their full paths,
 * else fails.
Path userHome = Paths.get(System.getProperty("user.home"))
Path downloadsFolder = userHome.resolve('Downloads')
assert Files.exists(downloadsFolder)

Pattern pattern = Pattern.compile("example-\\d+\\.pdf")

List<Path> files = Files.list(downloadsFolder)
	.filter({ p -> Files.isRegularFile(p) })
	.filter({ p -> pattern.matcher(p.getFileName().toString()).matches() })
if (files.size() > 0) {
	files.each { file ->
		println file.toString()
} else {
	KeywordUtil.markFailedAndStop("found no file that match regex ${pattern}")