Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
DISCLAIMER: I'm not a Groovy guy. I did not write this code. And it's not sitting in front of me, so is very pseudo.

I need to skip an item in a .eachFile list, but continue with the rest of the list.

What I have tried:

What did I try? Nothing. This is on a production system (no choice), so it really needs to be right from the first.

I'm pretty sure what I have below will work, but it's best if I get confirmation.

dir.eachFile (something)
{
	Cool stuff being done here
}  //on to the next file

What I need is

dir.eachFile (something)
{
   preliminary cool stuff

   if (Test condition)
     {
       finish doing cool stuff
     }
 //on to the next file
}

Or is something like this a better idea? I'm leaning toward the first way. Seems simpler, unless there's a reason for doing it differently.

dir.eachFile (something)
{
   preliminary cool stuff

   if (not Test condition)
     {
       skip this file and break/continue/return to top of loop
     }  //no else needed, if it jumps back to the top

   finish doing cool stuff
 //on to the next file
}


So, how would I skip the rest of the loop code and return to the top? I've seen multiple ways, but none of them specifically say they work with an eachFile method. Return? Continue? Break?. I think Break will just kill the whole thing, not skip, which would be a non-starter.

Ideally, the "bad" files would be moved to BadFile directory, not just skipped, using code in an "else" after the "if". Is Java/Groovy strict about things like that? I'm probably over-thinking...this is operating off a list, yes? Not touching the actual file unless it passes the test condition.
Posted
Updated 17-Dec-20 3:38am

1 solution

Java
dir.eachFile (something)
{
   preliminary cool stuff

   if (not Test condition)
     {
       continue; // skip this file and break/continue/return to top of loop
     }           //no else needed, if it jumps back to the top

   finish doing cool stuff
 //on to the next file
}

No idea about Groovy, but in most languages the continue statement means, skip the rest of this block and go to the top of the loop for the next iteration. You should check the documentation to make sure it is the right verb.
 
Share this answer
 
Comments
GenJerDan 17-Dec-20 10:06am    
That's my understanding of it, too. I just get nervous diddling with a live system. An example I found on the web has:
class Example {
static void main(String[] args) {
int[] array = [0,1,2,3];

for(int i in array) {
if(i == 2)
continue;
println(i);
}
}
}
in which everything gets printed except "2". I just wasn't sure if that would apply with a .eachFile loop, or if it's only good in vanilla "for" loops. That's from the documentation. S.O. is all over the place with answers, some saying "return", some saying "continue".
The divide seems to be "for" versus ".eachXXX" loops, with .eachXXX liking "return" best.
(I'll come back an Accept Solution once I try it out, if I don't get a more definitive answer. Thanks.)
Richard MacCutchan 17-Dec-20 10:16am    
I just found the following Groovy - Loops - Tutorialspoint[^], which explains ... well part of it. Does that mean it does not work in eachFile? The only way to find out is to test it. If it does not work then I guess you will just have to use if/else.
GenJerDan 18-Dec-20 6:15am    
Yeah, I went with if/else. Simple, if not fancy. And it's working, which is all that Higher cares about. LOL

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900