groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul King <pa...@asert.com.au>
Subject Re: why does this throw an exception? new to groovy, script works outside of class def
Date Wed, 29 Mar 2017 02:12:44 GMT
Your dir.eachFile expression needs to be inside a method, constructor
or initializer block.
Given the rest of your code, I'd be inclined to add an init method and
move the init code into that, e.g.:

  def init() {
    dir = new File(path)
    dir.eachFile ...
  }

Then add a f.init() line straight after your current Foo constructor call.

Cheers, Paul.

On Wed, Mar 29, 2017 at 11:42 AM, Petersen, Robert
<robert.petersen@rakuten.com> wrote:
> groovy> import groovy.io.FileType
>
> groovy> class Foo {
>
> groovy>       def java.lang.String path
>
> groovy>       def java.lang.String pattern
>
> groovy>       def java.lang.String replacement
>
> groovy>       def list = []
>
> groovy>
>
> groovy>       File dir = new File(path)
>
> groovy>
>
> groovy>         // dir.eachFileRecurse (FileType.FILES)  // gets subdirs
>
> groovy>         dir.eachFile( FileType.FILES)
>
> groovy>         {
>
> groovy>
>
> groovy>             file ->
>
> groovy>               if (file.name.endsWith("txt")) list << file
>
> groovy>         }
>
> groovy>   }
>
> groovy> // this works
>
> groovy> def f = new Foo(path: "L:\\tmp", pattern: "this", replacement:
> "that")
>
> groovy> println f.path
>
> groovy> println f.pattern
>
> groovy> println f.replacement
>
> groovy> //Afterwards the list variable contains all txt files (java.io.File)
> of the given directory (and its subdirectories with Recurse):
>
> groovy> f.list.each {
>
> groovy>   println it.path
>
> groovy> }
>
>
>
> 1 compilation error:
>
>
>
> unexpected token: dir at line: 13, column: 9

Mime
View raw message