0

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.

K.Isbruch
  • 145
  • 2
  • 8

1 Answers1

1

Use new String('string') or directly 'string' are equivalent.

The problem in your case is the typo in the hotinfoDownloadDir definition.

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 be new String('.\\hotinfo_5152'), if you change this, your code will work.

albciff
  • 18,112
  • 4
  • 64
  • 89
  • Thanks for this veeery quick response. But I do not get it. I compared the two code snippets you provided, and in my text editor the look exactly the same. Actually, I copied the literal for the directory from the first fileset line ```fileset(dir: '.\\hotinfo_5152', includes: '*.zip')``` to the line ```String hotinfoDownloadDir = new String('.\\hotinfo_5125')``` to make sure that they are exactly the same. – K.Isbruch Aug 29 '22 at 12:10
  • 1
    @K.Isbruch the folder name is incorrect in your String variable for the last attempt in your original code, the two last numbers are in different order:52 vs 25 – albciff Aug 29 '22 at 17:35