I am relatively new in groovy/java, and struggle with a simple task, to process all files following a given pattern in a given directory.
I tried this below, but as soon as try to use a variable instead of a fixed string for the directory of a fileset, it tells me that it doesn't find the directory - although it found it when entered as literal string.
My question, how to fix the error in line 34/32? My code:
import groovy.ant.AntBuilder
String hotinfoNumber = new String('5152');
String unzipSpec = new String('*.zip')
String hotinfoDownloadDir = new String('.\\hotinfo_5125')
String[] hotinfoUnzips
hotinfoUnzips = [ '*.zip' , 'H*.zip']
println ('GO, hotinfoNumber=' + hotinfoNumber + ', unzipSpec=' + unzipSpec + ', hotinfoDownloadDir=' + hotinfoDownloadDir + ', hotinfoUnzips=' + hotinfoUnzips)
AntBuilder ant = new AntBuilder();
println 'c+c'
ant.fileScanner {
fileset(dir: '.\\hotinfo_5152', includes: '*.zip')
}.each { File f ->
println "c+c - Found file ${f.path}"
}
println ("c+v")
hotinfoUnzips.each { unzipFilespec ->
println 'c+v-unzipFilespec=' + unzipFilespec
ant.fileScanner {
fileset(dir: '.\\hotinfo_5152', includes: unzipFilespec.toString())
}.each { File f ->
println "c+v - Found file ${f.path}"
}
}
println ("v+v")
hotinfoUnzips.each { unzipFilespec -> // line 32
println 'v+v-unzipFilespec=' + unzipFilespec
ant.fileScanner { // line 34
fileset(dir: hotinfoDownloadDir.toString(), includes: unzipSpec.toString())
}.each { File f ->
println "v+v - unzips-Found file ${f.path}"
}
}
and the result:
c+v - Found file G:\TESTS_SAMPLES\GROOVY\test_misc\hotinfo_5152\HotInfo_5125.zip
c+v-unzipFilespec=H*.zip
c+v - Found file G:\TESTS_SAMPLES\GROOVY\test_misc\hotinfo_5152\HotInfo_5125.zip
v+v
v+v-unzipFilespec=*.zip
Caught: G:\TESTS_SAMPLES\GROOVY\test_misc\hotinfo_5125 does not exist.
G:\TESTS_SAMPLES\GROOVY\test_misc\hotinfo_5125 does not exist.
at org.apache.tools.ant.types.AbstractFileSet.getDirectoryScanner(AbstractFileSet.java:512)
at DirAndFile2$_run_closure4.doCall(DirAndFile2.groovy:34)
at DirAndFile2.run(DirAndFile2.groovy:32)
Disconnected from the target VM, address: '127.0.0.1:57397', transport: 'socket'
Process finished with exit code 1
Any hints and suggestions are highly appreciated.
Use
new String('string')or directly'string'are equivalent.The problem in your case is the typo in the
hotinfoDownloadDirdefinition.Note that you change the order of the last two numbers in
new String('.\\hotinfo_5125')definition compared with the'.\\hotinfo_5152'.new String('.\\hotinfo_5125')must benew String('.\\hotinfo_5152'), if you change this, your code will work.