0

OK, your code is better to deal with the problem (comparing if it is a file). :) Anyway, below is a suggestion code to add to your existing code.

My code or the code you posted wth "File" objects....

0

Sorry that was a question :)

My code or the code you posted wth "File" objects....?

0

I should explain better than that... Ok, my approach is called Black list approach. What it meant is to reject if the incoming request is not in the list. Your approach is called White list approach which is the opposite -- to accept if the request is in the white list. In many cases, black list approach is unsafe because one may not know all incoming varieties. As a result, some unknown varieties may go through. Yours, on the other hand, will accept if and only if it is a file. It is also safer to add more acceptable conditions to the while list and remains safe; where as, there is no guarantee that it will be safe when add more conditions to rejected list. ;)

Edited by Taywin

0

I should explain better than that... Ok, my approach is called Black list approach. What it meant is to reject if the incoming request is not in the list. Your approach is called White list approach which is the opposite -- to accept if the request is in the white list. In many cases, black list approach is unsafe because one may not know all incoming varieties. As a result, some unknown varieties may go through. Yours, on the other hand, will accept if and only if it is a file. It is also safer to add more acceptable conditions to the while list and remains safe; where as, there is no guarantee that it will be safe when add more conditions to rejected list. ;)

OK I guess....

Anyways new code that still does not work :(

This is the same code as before except the switch to ASCII mode is included.

Basically still inserts the the file even if there are more than 44........

Ill comment because I dont feel like translating the variables OVER AND OVER again each time I post new code:

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

        int counter=1;
        FTPFile found;
        int done=0;
        FTPClient f=new FTPClient();
        System.out.println("Trying");
        f.connect("192.168.100.2");
        f.login("Anonymous","myname@emailserver.com");
        System.out.println("YES!");
        File e=new File("test.txt");
        FileInputStream s=new FileInputStream(e);

        FTPFile[] list = f.listFiles();
        String cai="CARPETAINTERNET"; //folder I want to use
        while (done==0)
        {
            found=null;
            if (counter==1)
            {
                cai="CARPETAINTERNET";
            }
            else
            {
                cai="CARPETAINTERNET"+Integer.toString(counter); 
            }

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


            if (found!=null)
            {
                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;
                    }
                }

                if (archivos<44)
                {
                    f.setFileTransferMode(FTP.ASCII_FILE_TYPE);
                    f.setFileType(FTP.ASCII_FILE_TYPE);
                    f.storeFile(f.printWorkingDirectory()+"/"+e.getName(), s);
                    done=1;
                }
                else
                {
                    f.changeToParentDirectory();
                    counter=counter+1;
                }


            }//end if found not null

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

        }//end while



    } //end main

Only things that happens is it inserts the file, less equal and/or more than 44 files in the directory.

Edited by JamesCherrill: removed real email address

0

OK, let's do a bit debugging...

1) Line 31, the condition should be below. You must not compare 2 string with == because you are not comparing their value but their object reference. Use equals() method every time you are comparing 2 strings.

if ((list[i].getType()==FTPFile.DIRECTORY_TYPE) && (list[i].getName().equals(cai)))

2) Before line 53, insert...

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

Then you will see the progress of the total file.

0

It might have been that:

if ((list[i].getType()==FTPFile.DIRECTORY_TYPE) && (list[i].getName().equals(cai)))

line as it now works :)

There is just now one problem :(

I was first told to store everything in C:/ I mean make C:/somefolder, C:/somefolder2, etc but now I have to start in a folder. Work from:

C:/startingfolder/

So Im not sure if change to parent directory changes to C:/ or C:/startingfolder . I am going to have to check that out....

0

Ive started my program by doing this:

f.changeWorkingDirectory("/");
        if (f.changeWorkingDirectory("/webfolder/")==false)
            {
                f.makeDirectory("/webfolder/");
                f.changeWorkingDirectory("/webfolder/");
            }

Do I have to put that / at the end of webfolder or is it

f.makeDirectory("/webfolder");

?

0

No, do not put "/" in front. The program will automatically try to create a folder from where its current working directory.

f.makeDirectory("aFolderName");

Edited by Taywin

0

New code:

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

        int counter=1;
        FTPFile found;
        int done=0;
        FTPClient f=new FTPClient();
        System.out.println("Trying");
        f.connect("192.168.100.2");
        f.login("Anonymous","");
        System.out.println("YES!");
        File e=new File("test2.txt");
        e.createNewFile();
        FileInputStream s=new FileInputStream(e);

        FTPFile[] list = f.listFiles();
        String cai="CARPETAINTERNET"; //folder I want to use
        f.changeWorkingDirectory("/");
        if (f.changeWorkingDirectory("webfolder")==false)
            {
                f.makeDirectory("webfolder");
                f.changeWorkingDirectory("webfolder");
            }

        while (done==0)
        {
            found=null;
            if (counter==1)
            {
                cai="CARPETAINTERNET";
                f.makeDirectory(cai);
                f.changeWorkingDirectory(cai);
            }

            else
            {
                cai="CARPETAINTERNET"+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)
                {
                    f.setFileTransferMode(FTP.ASCII_FILE_TYPE);
                    f.setFileType(FTP.ASCII_FILE_TYPE);
                    f.storeFile(f.printWorkingDirectory()+"/"+e.getName(), s);
                    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.makeDirectory(cai);
                f.changeWorkingDirectory(cai);


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

        }//end while



    } //end main

What this does is, if there are more than 44 files, it does

C:/webfolder/CARPETAINTERNET/CARPETAINTERNET/test2.txt

It should insert it into

C:/webfolder/CARPETAINTERNET2/test2.txt

It never reaches your working with and total files println.....

0

You added some code lines that actually broke the logic...

1)Move line 16 down to line 24. You should read the whole file list after you are at the intended base directory.

2)Below is the code portion to be removed. You are not supposed to make/change directory while you don't know whether it exists or is full.

if (counter==1)  {
  cai="CARPETAINTERNET";
  //f.makeDirectory(cai);   // remove!
  //f.changeWorkingDirectory(cai);  // remove!
}
else {
  cai="CARPETAINTERNET"+Integer.toString(counter);
  //f.makeDirectory(cai);  // remove!
  //f.changeWorkingDirectory(cai);  remove!
}
0

Still doesnt put it correctly :(

New code:

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

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




        //System.out.println(h.hello);

        FTPClient f=new FTPClient();
        System.out.println("Trying");
        f.connect("192.168.100.2");
        f.login("Anonymous","");
        System.out.println("YES!");
        File e=new File("test2.txt");
        e.createNewFile();
        FileInputStream s=new FileInputStream(e);

        //FTPFile[] list = f.listFiles();
        String cai="CARPETAINTERNET"; //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="CARPETAINTERNET";
                ///f.makeDirectory(cai);
                //f.changeWorkingDirectory(cai);
            }

            else
            {
                cai="CARPETAINTERNET"+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)
                {
                    f.setFileTransferMode(FTP.ASCII_FILE_TYPE);
                    f.setFileType(FTP.ASCII_FILE_TYPE);
                    f.storeFile(f.printWorkingDirectory()+"/"+e.getName(), s);
                    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);


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

        }//end while



    } //end main

This does the following:

I have webfolder created (c:/webfolder)

then I have c:/webfolder/carpetainternet with 60 files inside.

It detects it. your print comes out saying:

Working with /webfolder/CARPETAINTERNET/test2.txt
Total file: 60

BUT

it does C:/CARPETAINTERNET2/test2.txt when it should do c:/webfolder/carpetainternet2/text2.txt

Must be somewhere Im going back to the parent directory more than once....Right now I cant see it. Ill keep looking but if you see it before, please comment :)

Thanks for the help

1

Remove line 99 which changes back to parent directory before it create a new directory. The reason is that your program has already switched to the parent directory when it found the folder but the folder is full (from line 87).

Edited by Taywin

0

Remove line 99 which changes back to parent directory before it create a new directory. The reason is that your program has already switched to the parent directory when it found
the folder but the folder is full (from line 87).

I feel stupid; I didnt see that line :S

It looks like it is working (Ill do some more test now). This is just the base because now I have to port this small portion to a bigger program that deals with more subfolders/files. With this base, I could mold it to the bigger program but, like always, things never work out the first time....

Ill problably be back in a few days.

Thanks a lot for helping out Taywin.

0

Yup, it looks like it works :)

This afternoon Ill start to port this base over to the main program.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.