Im currently writing a spock test and need to add a wild card to a file path. the script should look into the subjects folder and into all the subfolders to find the advanced folder and look for exam.txt, however, i keep receiving an error saying FileNotFound.
I believe the code is correct as it can parse files fine but the wildcard bit throws the exception.
new File("School/Exams/Questions/Subjects/**/Advanced")
if(it.name.matches("questions.txt"){
print it
}
You can use FileNameFinder to get list of file names:
new FileNameFinder()
.getFileNames('School/Exams/Questions/Subjects/', '**/Advanced/questions.txt')
The java File class does not support wildcards which is why you are seeing the FileNotFoundException. The File class is trying to find a file with asterisk characters in the path name.
I think the groovy AntBuilder might be your friend here. Given the following groovy code:
new AntBuilder().fileScanner {
fileset(dir: '.', includes: '**/Advanced/questions.txt')
}.each { File f ->
println "Found file ${f.path}"
}
(note the use of the 'search at any depth' double star wildcard in the includes pattern)
and the following directory structure:
$ tree
.
├── one
│ └── ten
│ └── Advanced
│ └── questions.txt
├── three
│ └── thirty
│ └── Advanced
└── two
├── twenty
│ └── Advanced
│ └── questions.txt
└── twentyone
└── Advanced
the script yields:
Found file one/ten/Advanced/questions.txt
Found file two/twenty/Advanced/questions.txt
with the paths abbreviated for readability.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.