This discussion is archived
4 Replies Latest reply: Jan 28, 2013 9:08 AM by rp0428 RSS

Problem when looping through folders:

960150 Newbie
Currently Being Moderated
Problem when looping through folders:

I have a java class with the following code in its main method:
 String path = "/path/of/some/folder";
 String outFile = "/somepath/output.csv";
 getT gt = new getT();
 gt.performTask(path, outFile);
This class works perfectly every time I run it on a certain folder.

But now I want to loop through some folders to automate the process, hence created the following program:
    public getT_Loop(String folderPath)
    {   
        /* Loop through folders in directory folderPath */ 
        String subFolderName;
        String subFolderPath;
        File overlaysPath;
        File parentFolder = new File(folderPath);
        File[] listOfFiles = parentFolder.listFiles();
        
        for (int i = 0; i < listOfFiles.length; i++) 
        {
            if (listOfFiles.isDirectory()) 
{
subFolderName = listOfFiles[i].getName();
subFolderPath = listOfFiles[i].getAbsolutePath();
overlaysPath = new File(subFolderPath + "/Overlays");
if (overlaysPath.exists() )
{
/* for every overlays folder perform getT */
try{
getT check = new getT();
check.performTask(overlaysPath.getAbsolutePath(),
subFolderPath + "/output.csv");
}
catch(Exception e ){e.printStackTrace();}
}
}
}
}
public static void main(String args[])
{
String folderPath = "/somePath";
getT_Loop all = new getT_Loop(folderPath);
}
}


This program works fine for two folders, then it halts at writing the output file for the third folder. Running the program separately for every folder works fine. Is there any trick around to overcome this problem? And what might the cause be? I doubt it is something about clearing the files from cache, is there any code that clears all data after each process, and then starts as if I am closing manually the program and running it again?

Thanks in Advance
Best Regards,
M. Tleis

Edited by: 957147 on Jan 27, 2013 4:45 AM

Edited by: 957147 on Jan 27, 2013 4:46 AM

Edited by: 957147 on Jan 27, 2013 4:47 AM

Edited by: 957147 on Jan 27, 2013 4:49 AM
  • 1. Re: Problem when looping through folders:
    baftos Expert
    Currently Being Moderated
    If you want to recursively list the files in a directory (similar to the Windows command "dir some_dir /s"), you have plenty of info here: http://stackoverflow.com/questions/2056221/recursively-list-files-in-java.
  • 2. Re: Problem when looping through folders:
    960150 Newbie
    Currently Being Moderated
    Dear Baftos,

    Thank you for your reply;

    I am already able to loop recursively through files. Sorry if my explanations were not clear enough.

    I will try to state my question in another forms:

    How Can I loop through a piece of code, as if I am running that code for the first time. (i.e. How can I clear all the variables and data and start a new clean instance).

    Best Regards,
    M. Tleis
  • 3. Re: Problem when looping through folders:
    Kayaman Guru
    Currently Being Moderated
    957147 wrote:
    How Can I loop through a piece of code, as if I am running that code for the first time. (i.e. How can I clear all the variables and data and start a new clean instance).
    Ouch, you missed the basics of programming.

    You can use a method with local variables, they'll be empty every time you enter the method.
  • 4. Re: Problem when looping through folders:
    rp0428 Guru
    Currently Being Moderated
    >
    How Can I loop through a piece of code, as if I am running that code for the first time. (i.e. How can I clear all the variables and data and start a new clean instance).
    >
    Simple - the scope of variables is limited to the block that the variable is in

    1. define a new block for that piece of code using { and }.

    2. inside the block reinitialize the variables that the code in the block uses

    3. don't use any variables that are NOT initialized at the start of the block

    This sample code should show you the concept. It has a variable 'j' that is used outside the block to contain the loop iteration number and is used inside the block to contain the value 47.

    Note that setting the variable to 47 inside the block has no effect on the value that was set outside the block.
    public class Test3 {
    
        public static void main(String[] args) {
      
            for (int i = 0; i < 5; i++)
            {
                int j;
                j = i;
                System.err.println("Outside j = " + j);
                {   // this begins a new block
                    j = 47;  // initialize ALL variables used inside the block here - don't use ANY vars that are not initialized at the block start
                    System.err.println("Inside j = " + j);
                }   // this ends the new block - the 'inside' value of 'j' no longer exists
            }
        }
    }
    
    Outside j = 0
    Inside j = 47
    Outside j = 1
    Inside j = 47
    Outside j = 2
    Inside j = 47
    Outside j = 3
    Inside j = 47
    Outside j = 4
    Inside j = 47

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points