Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm working an example to list directory contents. When I process each directory the code works. Now I'd like to put both directories in an array and iterate the array. I get an error when I declare 'dir_c' because I've not given it a directory name which will change.

  def outfile = new File('Y:/TEMP/dirlist.txt')
  def dir_a = new File('z:/pathA/pathB')
  def dir_b = new File('z:/pathA/pathQ')
  // def dir_c = new File() //causes error ==>Could not find which method <init>(to invoke from this list
outfile.write('')

 dir_a.eachFile {
  if (it.isFile()) {
  outfile.append(it.name + '\n')
  }

  dir_b.eachFile {
   if (it.isFile()) {
    outfile.append(it.name + '\n')
   }
  // Up to this point code works

   def searchDir = ["z:/pathA/pathB","z:/pathA/pathQ"]
   searchDir.each{
  //was thinking using  ==>dir_c = it 
  // then carry on like singleton cases
  // but I get error message when I declare dir_c
  }
share|improve this question

1 Answer

up vote 2 down vote accepted

Do you mean like this:

def searchDir = ["z:/pathA/pathB","z:/pathA/pathQ"]
searchDir.each { d ->
    new File( d ).eachFile {
        if (it.isFile()) {
            outfile.append( "$it.name\n" )
        }
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.