Hi

This is related to 3 threads:

http://www.daniweb.com/software-development/java/threads/438729/recursive-ftp-listingmakingcreatingetc-access
http://www.daniweb.com/software-development/java/threads/439941/recursive-ftp-listingmakingcreatingetc-access...-part-2
http://www.daniweb.com/software-development/java/threads/442142/recursive-ftp-listingmakingcreatingetc-access...-part-3

At first, I had NO clue where to even start. Over a couple of weeks, Ive slowly but surely started to make the process correctly but Im stumpped so (for the third time) ive thought of rewriting everything from sratch.

I only have two functions right now: createfolder and insertfile. Nothing else.

My requirements are the following:

Random number is a number common to process A and B
My main path is C:/ we base everything off that.
In C:/, we see if a folder called Webfolder exists. If it does, we enter it. If it doesnt, we created it.
Once it is created and found, we enter it.

Process A: Counter is a local variable that equals 1. It is local to Process A. I make a folder with the current date and "" and the counter ONLY if there are less than 44 file inside this webfolder. If there is more than 44 files, I add one to a counter and then make a folder with the current date and the "" and the counter as long as it has less than 44 files inside. If not process A is repeated until counter is 9.

Once Process A is completed, I change to that that current date and "_" and the counter folder and inside make a folder with a random number.

Next, we have to insert some files into these folders. I once again start my path off C:/ then enter Webfolder (Webfolder at this point always exists because I call the folder creating function ALWAYS before inserting a file).

Process B: Counter is a local variable that equals 1. It is local to Process B. I search for a folder with the current date and "_" and the counter. If I find it, I search if it has less than 44 files inside. If it does have more 44, I add to counter plus one to look for the next folder which does (or doesnt) have more than 44 files.
If it doesnt, I insert a random numer BMP inside of it.

Also if it doesnt, I create a folder with the name of random number, same as Process A. I go into that folder and create whatever file I pass to the function.

Basically, I believe that it is.

Any help, thank you very much.

Your requirement is still unclear to me.

1)Are process A and B running concurrently?
2)If I understand correctly, the job for process A is to create folders and for process B is to create & insert files?
3)If that's the case for #2, do you have another class that tie (relate) process A and B together?
4)Does your program require to keep running all the time (24/7)?
5)If you answer "yes" in #4, did you implement process A & B as Threads?
6)Would there be multiple process A and B running at the same time?
7)Would process B randomly create & insert a file into any matched folder it found if all of the criterias are met? i.e. There exists 4 folders -- 20121214_1, 20121214_2, 20121214_3, 20121214_4 -- and each folder contains file number as 44, 44, 43, 2 respectively. Process B could have a chance to create an insert a file in either 20121214_3 or 20121214_4? Or process B will always create & insert a file in sorted order (in this case, folder 20121214_3 will always be selected first)?
8)Do you need to have process A and B separated?

By process I ment functions............They arent seperete process via thread and such. Damn, sorry for that wrong term

1: No. FUNCTION A which needs to create folders before FUNCTION B inserts filres
2: Exactly. FUNCTION B creates inserts files into folders made by FUNCTION A
3: A main. My program is this (obviously testing purposes):

public static void main(String[] args) throws SocketException, IOException 
    {
        int i=0;
        String ip="192.168.100.2";
        for (i=0;i<50;i++)
        {
            Random random = new Random(System.nanoTime()); 
            String nombre=String.valueOf(random.nextInt(10000000));



            hacercarpetaversiontres(nombre,ip); //FUNCTION A




            File b=new File(nombre+".bmp");
            b.createNewFile();
            meterarchivonuevaversion(nombre+".bmp",nombre,ip); //FUNCTION B
            b.delete();


            File j=new File(nombre+".jpg");
            j.createNewFile();
           meterarchivonuevaversion(nombre+".jpg",nombre,ip); //FUNCTION B
            j.delete();


            File x=new File(nombre+".bin");
            x.createNewFile();
            meterarchivonuevaversion(nombre+".bin",nombre,ip); //FUNCTION B
            x.delete();

            File p=new File("Path1.bin");
            p.createNewFile();
            meterarchivonuevaversion("Path1.bin",nombre,ip); //FUNCTION B
            p.delete();
        }





    } //end main

4: No. It is called upon
5: NA
6: Could be but at the same same same SAME time, unlikely.
7: No. It would insert a file into if it is first (20121214_1 comes/is created before 20121214_2) and that folder has less than 44 files inside of it. After that, next "set" of files
8: Function A always runs before function B

There are still questions regarding your approach.
1)Is your program keep the folder counter at all time? If so, is the value accessible by both function A and B? Or do you want to keep counter value separately for both? If you are thinking about keeping each function's local value, I will tell you that don't. The value should be from the same variable for both function A and B to access. If you keep them separated, you will have problem keeping track of your program states. Besides, it is a bad idea to decouple functions because both functions are directly related.
2)Why do you have to generate a random number for folder once the counter gets up to 9? Why don't you keep the counter going without a limit? Please enlighten me.

By the way, below is a pseudo code for the portion of searching & creating folders. It is the same as I suggested in your earliest post about doing so. However, I give you a recursive style with returning type is arbitrary (string, file system, etc). This approach is not very efficient because it will always start looking for the correct folder as if no folder has been created. I show this to you because it seems to be easier in my thought process; besides, you should think about optimization later (after you correctly implement it).

/*
FOLDER_COUNTER <- 0  // constant for recursive folder look up
mainFolderName <- A_MAIN_FOLDER_NAME

function insertFileToFolder
  if mainFolderName is not found
    create mainFolderName
  end if
  step into mainFolderName
  lookupFolder <- get current date string formatted as YYYYMMDD
  existFolder <- lookUpForFolder(mainFolderName, lookupFolder, FOLDER_COUNTER)
  step into existFolder
  create and insert a new file to the folder
end function


function lookUpForFolder    // recursive function
  currentLookupFolder <- lookupFolder
  if folderCounter is greater than 0
    concatenate currentLookupFolder with folderCounter
  end
  if mainFolderName/currentLookupFolder exists
    step into mainFolderName/currentLookupFolder
    totalFile <- count total file in the folder
    if totalFile is greater than or equal to 44
      step out to its parent folder
      increment folderCount by 1
      return lookUpForFolder(mainFoldername, lookupFolder, folderCounter)
    else
      return mainFolderName/currentLookupFolder
    end if

  else  // not exist, create a new one
    create mainFolderName/currentLookupFolder
    return mainFolderName/currentLookupFolder
  end
end function
*/

There are still questions regarding your approach.
1)Is your program keep the folder counter at all time? If so, is the value accessible by both function A and B? Or do you want to keep counter value separately for both? If you are thinking about keeping each function's local value, I will tell you that don't. The value should be from the same variable for both function A and B to access. If you keep them separated, you will have problem keeping track of your program states. Besides, it is a bad idea to decouple functions because both functions are directly related.
2)Why do you have to generate a random number for folder once the counter gets up to 9? Why don't you keep the counter going without a limit? Please enlighten me.

1) Yes, it will keep the folder counter all the time. The value is accessible and used by both functions BUT the variables are local to each function. Im not sure how to proceed using a local variable to the main function and use it between function a and b chaning values
2) Length issues (always 10 characters). The folders will be 20121512_1 to 20121512_9 ; Obviouly 10 makes it 11 characters. This is something someone else needs to use and program to as well so it has to stay 10 chars.

By the way, below is a pseudo code for the portion of searching & creating folders. It is the same as I suggested in your earliest post about doing so. However, I give you a recursive style with returning type is arbitrary (string, file system, etc). This approach is not very efficient because it will always start looking for the correct folder as if no folder has been created. I show this to you because it seems to be easier in my thought process; besides, you should think about optimization later (after you correctly implement it).

Personally Im not your "efficient" programmer. I just want things to work. I get back to work on Monday so Ill try to implement this then. I just don't see where WEBFOLDER is created...

For #1, that's why you should never separate the value and kept in 2 separated local variables. Keep the value out side and pass through the call, or keep no value but let the call figure it out.

For #2, then why need underscore? If it is going to be 10 maximum, don't use up your precious count with not that meaningful character. Then you will have now 99 folders.

/*
i.e. for prefix after the date only
00, 01, 02, ..., 99
*/

If that's not enough, move on too letters too. The total number of maximum folder will increase a lot. You will get 36 x 36 which is equal to 1296 folders.

/*
i.e. for prefix after the date only
00, 01, 02, ..., 0A, 0B, 0C, ... 0Z, 10, 11, 12, ..., 9Z, A0, A1, ..., ZZ
*/

To answer your question, you should see that it is at the beginning of my pseudo code -- A_MAIN_FOLDER_NAME.

For #1, that's why you should never separate the value and kept in 2 separated local variables. Keep the value out side and pass through the call, or keep no value but let the call figure it out. For #2, then why need underscore? If it is going to be 10 maximum, don't use up your precious count with not that meaningful character. Then you will have now 99 folders.

OK Ill attempt based on your pseudocode implement it with the value on the outside....

Even if I reach 99, the maximum that can be processed is 44 so........Also Ive been specified to do it this way (with underscore) and only up to 9. 891 files in total is MORE than enough.

To answer your question, you should see that it is at the beginning of my pseudo code -- A_MAIN_FOLDER_NAME.

I think your pseudocode is missing a lot of things (or at least some don't make sense) but I imagine this being intentional. I might even implement it now and see how it goes.

Got this for now (not much but...)

package recursivo;

import java.io.File;
import java.io.IOException;
import java.net.SocketException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.net.ftp.FTPClient;

public class recursivo 
{

    /**
     * @param args
     */
    public static int counter=1;

    public static void main(String[] args) 
    {

            int i=0;
            String ip="127.0.0.1";
            for (i=0;i<50;i++)
            {
                Random random = new Random(System.nanoTime()); 
                String name=String.valueOf(random.nextInt(10000000));



                lookupforfolder(name,ip); //FUNCTION A




                File b=new File(name+".bmp");
                b.createNewFile();
                insertfiletofolder(counter,name+".bmp",ip); //FUNCTION B
                b.delete();


                File j=new File(name+".jpg");
                j.createNewFile();
               insertfiletofolder(counter,name+".jpg",ip); //FUNCTION B
                j.delete();


                File x=new File(name+".bin");
                x.createNewFile();
                insertfiletofolder(counter,name+".bin",ip); //FUNCTION B
                x.delete();

                File p=new File("Path1.bin");
                p.createNewFile();
                insertfiletofolder(counter,"Path1.bin",ip); //FUNCTION B
                p.delete();
            }


        } //end main

    public static void insertfiletofolder(int counter,String filename,String ip)
    {
        FTPClient f=new FTPClient();
        try {
            f.connect(ip);
            f.login("a","");
            f.changeWorkingDirectory("/");
            if (f.changeWorkingDirectory("WEBFOLDER")==false)
            {
                f.makeDirectory("WEBFOLDER");
                f.changeWorkingDirectory("WEBFOLDER");
            }

            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
            Date date = new Date();
            String currentdate=dateFormat.format(date);
            currentdate=currentdate.replace("/","");
            currentdate=currentdate+"_"+Integer.toString(counter); // Is this: lookupFolder <- get current date string formatted as YYYYMMDD
            // existFolder <- lookUpForFolder(mainFolderName, lookupFolder, FOLDER_COUNTER)
            f.changeWorkingDirectory(/*step into existFolder*/);
            f.storeFile(/*step into existFolder*/+"/"+filename, filename);


        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } //end insertfiletofolder


} //end class

I understand some of what you wrote but I just do not know how to implement it.

OK, if you want it that way.

My pseudo code is not exactly describing your A & B. The lookUpForFolder is a little combination of your A & B. It looks up a proper folder AND create it if not found, and then return the location which is ensured that it will be qualified for file insertion. There is no detail for your B in my pseudo code because it is just a simple file creation in the returned valid location from lookUpForFolder.

OK, if you want it that way. My pseudo code is not exactly describing your A & B. The lookUpForFolder is a little combination of your A & B. It looks up a proper folder AND create it if not found, and then return the location which is ensured that it will be qualified for file insertion. There is no detail for your B in my pseudo code because it is just a simple file creation in the returned valid location from lookUpForFolder.

So then lookupfolder just gives me the route that I have to insert the file to? But how do I create that folder in the first place knowing it exists?

Also, It doesnt have to be recursive BTW; I dont mind making 50 inefficient functions as long as I understand the code....

Is there any way to do this non recursive which I would perfer?

So then lookupfolder just gives me the route that I have to insert the file to? But how do I create that folder in the first place knowing it exists?

I am not sure that you actually understood my pseudo code at all... The function create a folder if it doesn't exist and returns it or returns the one it found which contains less than 44 files... You could simply change it to iterative function instead.

/*
function lookUpForFolder(mfn, lf, fcnt)    // iterative function
  while true  // ASSUME that a folder will ALWAYS be found or created
    filePath <- mfn/lf
    if fcnt is greater than 0
      concatenate the path of filePath with "_" and fcnt
    end if
    if filePath exists
      totalFile <- count total file in the folder
      if totalFile is greater than or equal to 44
        increment fcnt by 1
      else
        return filePath
      end if
    else  // not exist, create a new one
      create filePath  // see a folder is created?
      return filePath
    end if
  end while
  return null
end function
*/

Watch out when you deal with file count increment. If you are going to keep File object all the time without recreating (using step in & out instead), you will need to make sure that you are stepping in and out correctly.

PS: Not to offend you, what degree do you have in? It seems to me that you don't really have programming concept at all.

Solved :) Finally I found a way to do correctly what I wanted.

PS: Not to offend you, what degree do you have in? It seems to me that you don't really have programming concept at all.

Offensive but nonetheless Im a software engineer. I just really hate programming in general, more so Java.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.