Can wildcards be use for deleting 2 or more files?

Hi @Brandon_Hein,
I was reading this post and wondered what you thought of this query.

Regarding deleting files, could I use wildcards to delete a subset of files.
For example, I only want to delete the 2 following Bobs*.txt files from my local folder.

BobsTestFile.txt
BobsTestFile2.txt
MarksTestFile.txt

def name = '*.Bobs.*' //when I use this the deletion does NOT work.
//def name = 'BobsTestFile' //when I use this the delete DOES work.
File file = new File(('C:/Users/OURDELL/Downloads/tmp/' + name + '.txt'))
println("file: "  +  file)
for (int cnt = 0; cnt < 2; cnt++) {
	println("cnt: "  +  cnt)
    if (file.exists()) {
       file.delete()
    }
}

Test Case:

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.stream.Collectors

import org.apache.commons.io.FileUtils

import com.kms.katalon.core.configuration.RunConfiguration

/* 
 * prepare the test fixture
 */
Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path fixtureDir = projectDir.resolve("Include/fixtures/sampleFiles")
assert Files.exists(fixtureDir)
Path workDir = projectDir.resolve("tmp");
if (Files.exists(workDir)) {
	FileUtils.deleteDirectory(workDir.toFile())
}
Files.createDirectories(workDir)
FileUtils.copyDirectory(fixtureDir.toFile(), workDir.toFile())
println "## files before deletion"
Files.list(workDir).forEach({p -> println projectDir.relativize(p) })

/*
 * delete Bob's files
 */
List<Path> toBeDeleted = Files.list(workDir)
						.filter({p -> p.toFile().isFile() })
						.filter({p -> p.getFileName().toString().matches("^Bob.*") })
						.collect(Collectors.toList())

println "toBeDeleted.size()=" + toBeDeleted.size()
assert toBeDeleted.size() > 0

toBeDeleted.forEach { p ->
	println "deleting ${projectDir.relativize(p)}"
	p.toFile().delete();
}

println "## files after deletion"
Files.list(workDir).forEach({p -> println projectDir.relativize(p)})

Please note that this code has a line with a regular expression (a sort of wildcard):

		.filter({p -> p.getFileName().toString().matches("^Bob.*") })

Output:

2022-12-11 11:36:49.074 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2022-12-11 11:36:49.093 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/Dave_Evers_wants_to_delete_files_filtering_by_wildcard
## files before deletion
tmp/BobsTestFile.txt
tmp/BobsTestFile2.txt
tmp/MarksTestFile.txt
toBeDeleted.size()=2
deleting tmp/BobsTestFile.txt
deleting tmp/BobsTestFile2.txt
## files after deletion
tmp/MarksTestFile.txt
2022-12-11 11:36:50.028 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/Dave_Evers_wants_to_delete_files_filtering_by_wildcard

Hi @kazurayam,
Thank you for your detailed response.
I can see the through thought behind your logic.
It’s an excellent solution.

Cheers,
Dave