This a followup to these two 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

For the "real thing" Ive decided to break it to two function: Insert file and make directory.

Ill reexplain what I am trying to do.

Im going to go a folder called "webfolder".
If it isnt found, it is created.

If it is found and a folder with the current date isnt isnide of it, a folder is created inside with the current date (a folder inside webfolder would be created called 12042012).

If that folder with the current date exists,
if there is more than 44 files inside,
then a second folder inside of webfolder would be created instead with a number (a folder inside webfolder would be created called 12042012_1 instead).

if it the folder with the current date does not have more than 44 files inside, a bmp file with a generated random num is created and then folder with that same num is created (so inside of webfolder and inside the current date foler, another folder with a generate random num). Inside this folder two files will be created.

The good news is that the code works 99.9% :) So thats reliving. The bad news is that when it detects that 44 limit, it puts it in the wrong place....

Its something like:

C:
------> webfolder
----------------->currentdate
--------------------------------------------->randomnumber
---------------------------------------------------------->file1
---------------------------------------------------------->file2
----------------------------->randomnumberbmp
--------------------------------------------->randomnumber1
---------------------------------------------------------->file1of1
---------------------------------------------------------->file2of1
----------------------------->randomnumber1bmp
--------------------------------------------->randomnumber2
---------------------------------------------------------->file1of2
---------------------------------------------------------->file2of2
----------------------------->randomnumber2bmp
etcetc
--------------------------------------------->randomnumber43
---------------------------------------------------------->file1of43
---------------------------------------------------------->file2of43
----------------------------->randomnumber43bmp
----------------->currentdate1
----------------------------->file1of44
----------------------------->file2of44
--------------------------------------------->randomnumber45
---------------------------------------------------------->file1of45
---------------------------------------------------------->file2of45
----------------------------->randomnumber45bmp
--------------------------------------------->randomnumber46
---------------------------------------------------------->file1of46
---------------------------------------------------------->file2of46
----------------------------->randomnumber46bmp

Recommended Answers

All 10 Replies

Posting code now.....

Any clearing up just ask because since it is SO big Ive split it up like I mentioned. Ill try to add comments....

public class recursiveftp
{

public static void meterarchivo(String elarchivo,String elnombredelprograma) throws SocketException, IOException 
    {

        int counter=1;
        FTPFile found;
        int done=0;

        String filetest=elarchivo;

        String extension=filetest;

        extension=extension.substring(extension.indexOf("."),extension.indexOf(".")+4);

        String nombreprograma=filetest;


        FTPClient f=new FTPClient();
        System.out.println("Trying");
        f.connect("192.168.100.2");
        f.login("something","soemth");
        System.out.println("YES!");
        File e=new File(filetest);
        //e.createNewFile();
        FileInputStream s=new FileInputStream(e);

        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        String fechaactual=dateFormat.format(date);
        fechaactual=fechaactual.replace("/","");


        String cai=fechaactual; //folder I want to use
        f.changeWorkingDirectory("/"); 
        f.changeWorkingDirectory("webfolder");  

        FTPFile[] list = f.listFiles();

        while (done==0)
        {

            found=null;
            if (counter==1)
            {
                cai=fechaactual;
                ///f.makeDirectory(cai);
                //f.changeWorkingDirectory(cai);
            }

            else
            {
                cai=fechaactual+Integer.toString(counter); 
                //f.makeDirectory(cai);
                //f.changeWorkingDirectory(cai);
            }

            for (int i=0;i<list.length;i++)
            {
                if ((list[i].getType() == FTPFile.DIRECTORY_TYPE) && (list[i].getName().equals(cai)))
                {
                    found=list[i];
                    break;
                }
            }


            if (found!=null)
            {
                //f.changeWorkingDirectory("/"+found.getName());

                //f.changeWorkingDirectory("/webfolder/"+found.getName());

                f.changeWorkingDirectory(found.getName());



                FTPFile[] list2 = f.listFiles();
                int archivos=0; //number of files
                for (int x=0;x<list2.length;x++)
                {
                    if (list2[x].getType()==FTPFile.FILE_TYPE)
                    {
                        archivos=archivos+1;
                    }
                }

                System.out.println("Working with "+f.printWorkingDirectory()+"/"+e.getName());
                System.out.println("Total file: "+archivos);
                if (archivos<44)
                {

                    if (extension.equals(".bmp"))
                    {
                        System.out.println("Soy un bmp");
                        f.setFileTransferMode(FTP.BINARY_FILE_TYPE);
                        f.setFileType(FTP.BINARY_FILE_TYPE);
                        f.storeFile(f.printWorkingDirectory()+"/"+e.getName(), s);
                        done=1;

                    }
                    else
                    {
                        f.changeWorkingDirectory(f.printWorkingDirectory());
                        System.out.println("nombreprograma es  " + nombreprograma);
                        f.setFileTransferMode(FTP.BINARY_FILE_TYPE);
                        f.setFileType(FTP.BINARY_FILE_TYPE);
                        System.out.println("estoy en " + f.printWorkingDirectory());

                        f.storeFile(f.printWorkingDirectory()+"/"+elnombredelprograma+"/"+e.getName(), s);
                        //f.rename(f.printWorkingDirectory()+"/"+e.getName(), f.printWorkingDirectory()+"/"+nombreprograma+"/"+e.getName());    
                        done=1;
                    }


                }
                else
                {
                    System.out.println("Cambio al parent directory");
                    f.changeToParentDirectory();
                    //f.changeWorkingDirectory("/webfolder/");
                    counter=counter+1;
                }


            }//end if found not null

            else //if found equals null
            {
                //f.makeDirectory("/"+cai);
                //f.changeWorkingDirectory("/"+cai);
                //f.changeToParentDirectory();
                f.changeWorkingDirectory(cai);
                f.storeFile(f.printWorkingDirectory()+"/"+e.getName(), s);
                done=1;
            }

        }//end while



        f.logout();
        f.disconnect();

    } //meterarchivo

    public static void hacercarpeta(String nombre) throws SocketException, IOException 
    {

        int counter=1;
        FTPFile found;
        int done=0;

        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        String fechaactual=dateFormat.format(date);
        fechaactual=fechaactual.replace("/","");

        String nombreprograma=nombre;

        FTPClient f=new FTPClient();
        System.out.println("Trying");
        f.connect("192.168.100.2");
        f.login("Asdfs","afdfd");
        System.out.println("YES!");


        String cai=fechaactual; //folder I want to use
        f.changeWorkingDirectory("/"); 
        if (f.changeWorkingDirectory("webfolder")==false)
            {
                f.makeDirectory("webfolder");
                f.changeWorkingDirectory("webfolder");
            }

        FTPFile[] list = f.listFiles();

        while (done==0)
        {

            found=null;
            if (counter==1)
            {
                cai=fechaactual;
                ///f.makeDirectory(cai);
                //f.changeWorkingDirectory(cai);
            }

            else
            {
                cai=fechaactual+Integer.toString(counter); 
                //f.makeDirectory(cai);
                //f.changeWorkingDirectory(cai);
            }

            for (int i=0;i<list.length;i++)
            {
                if ((list[i].getType() == FTPFile.DIRECTORY_TYPE) && (list[i].getName().equals(cai)))
                {
                    found=list[i];
                    break;
                }
            }


            if (found!=null)
            {
                //f.changeWorkingDirectory("/"+found.getName());

                //f.changeWorkingDirectory("/webfolder/"+found.getName());

                f.changeWorkingDirectory(found.getName());

                FTPFile[] list2 = f.listFiles();
                int archivos=0; //number of files
                for (int x=0;x<list2.length;x++)
                {
                    if (list2[x].getType()==FTPFile.FILE_TYPE)
                    {
                        archivos=archivos+1;
                    }
                }

                System.out.println("Total file: "+archivos);
                if (archivos<44)
                {
                    System.out.println("Carpeta neuva");
                    f.makeDirectory(nombreprograma);
                    done=1;
                }
                else
                {
                    f.changeToParentDirectory();
                    //f.changeWorkingDirectory("/webfolder/");
                    counter=counter+1;
                }


            }//end if found not null

            else //if found equals null
            {
                //f.makeDirectory("/"+cai);
                //f.changeWorkingDirectory("/"+cai);
                //f.changeToParentDirectory();
                f.makeDirectory(cai);
                f.changeWorkingDirectory(cai);

                done=1;
            }

        }//end while



        f.logout();
        f.disconnect();

    }  //hacercarpeta

public static void main(String[] args) throws SocketException, IOException 
    {

        int i=0;
        for (i=0;i<50;i++)
        {
            Random random = new Random(); // uses System.nanoTime() as seed
            String nombre=String.valueOf(random.nextInt(10000000));
            System.out.println("El nombre es: " + nombre);

            //procesocompleto();
            hacercarpeta(nombre);
            //hacercarpeta(nombre);




            File j=new File(nombre+".jpg");
            j.createNewFile();
            meterarchivo(nombre+".jpg",nombre);
            j.delete();

            File b=new File(nombre+".bmp");
            b.createNewFile();
            meterarchivo(nombre+".bmp",nombre);
            b.delete();

            File x=new File(nombre+".bin");
            x.createNewFile();
            meterarchivo(nombre+".bin",nombre);
            x.delete();

            File p=new File("Path1.bin");
            p.createNewFile();
            meterarchivo("Path1.bin",nombre);
            p.delete();
        }





    } //end main


}

"meterarchivo" is when I am inserting/making/create/etc a file.
"hacercarpeta" is when I am creating the folder structure.

meterarchivo has 2 parameters. The first one is the actual file I want to insert. The second is where exactly I want to insert it. This is the end route (not webfolder, it would ultimatly be the random number which is the last folder)

hacercarpeta only has one which is the name of the folder I want to create...

The main just runs a 50 time for loop so it can make 50 "groups of " files to test this out.

Also, I mess around with the extension but thats for another issue. When I fix this one, Ill move onto the extension bit.

Anything that isnt understood please comment so I can give more information.

Anyone?

No ideas at all? In the first two threads, you guys really helped and pretty much solved it...

Nothing still. Now it directly "swallows" it (it disappears)....Very odd.

Now it swallows the first one of the second folder.....Very annoying. Thought that Daniweb would have enough knowledge to know how to fix this.

Anyways Im problably rewriting both functions today, from scratch.

Rewrote it and similar problems. This cant be this difficult to do....

Don't have time right now... Will try to take a look at it tomorrow...

PS: I usually do not go through posts that past the first page. Sometimes I would go through on 2nd or 3rd page but that's it. If your post is bumped down way too far, I may not see it.

Don't have time right now... Will try to take a look at it tomorrow... PS: I usually do not go through posts that past the first page. Sometimes I would go through on 2nd or 3rd page but that's it. If your post is bumped down way too far, I may not see it.

Better yet, Im going to start a new thread with fresh ideas on how to approch this. Maybe thats what I need....

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.