Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

grails - how to handles if-else in groovy?

Here I'm passing the skipfolders variable in input if the skipfolders is true then it prints all files from Parent path and skip the sub folders. otherwise it returns all files from all folders include sub folders as well. Here I wrote if-else conditions. When I execute this code in FileMaker it executes without any errors and displayed result.but the if-else conditions does't working here.

Problem :

If-else conditions doesn't working here.it prints all files from FTP include subfolders.skipfolders condition doesn't working. at this return allFiles.join(' ') + ' '+ allFolderFiles.join(' ')+ ' ' prints directly and skipfolders condition doesn't working. Please help how to use if-else conditions properly in fileMaker groovy.

start()
def start(){

        boolean skipfolders = false
        def store;
        def ftpClient = new FTPClient()
        ftpClient.connect(server)
        // println(ftpClient.replyString)
        ftpClient.login(user,pass)
        ftpClient.enterLocalPassiveMode()
        FTPFile[] fileslist = ftpClient.listFiles("/")
        FTPFile[] folderfileslist = ftpClient.listFiles("/sample")

  if(skipfolders == false){

       def allFiles = []; 
       for(int i=0; i<fileslist.length; i++){  
       String file_name = fileslist[i].getName()
       String file_timestamp = fileslist[i].getTimestamp().getTime()     
       String s = '|' + file_name+ '|' + '/' +file_name+'|'  +file_timestamp
       allFiles << s       
   }  
      def allFolderFiles = [];
      for(int i=0; i<folderfileslist.length; i++){
      String folderfile_name = folderfileslist[i].getName()
      String folderfile_timestamp = folderfileslist[i].getTimestamp().getTime()
      String s1 = '|' +folderfile_name+ '|' + '/sample' +'|'+folderfile_name+'|'  +folderfile_timestamp
      allFolderFiles << s1
 }
  ftpClient.disconnect()
  return allFiles.join('
') + '
'+ allFolderFiles.join('
')+ '
'

}
else{
       def allFiles = []; 
       for(int i=0; i<fileslist.length; i++){  
       String file_name = fileslist[i].getName()
       String file_timestamp = fileslist[i].getTimestamp().getTime()     
       String s = '|' + file_name+ '|' + '/' +file_name+'|'  +file_timestamp
       allFiles << s       
   }  
   ftpClient.disconnect()
   return allFiles.enter code herejoin('
')
  }
}

    enter code here

if anybody having idea please let me know thanks.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If i am understanding the question correctly, you want to set the skipfolders variable as a parameter.

As it is you have declared:

boolean skipfolders = false;

So the else is never reached as mentioned by user daggett in the question comment.

If you do something like this instead:

start(true) or start(false)
def start(boolean input){
    boolean skipfolders = input;
    ...
}

Then you can reach the else statement depending on your input.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...