Hello

Im trying to do some basic FTP work.

What I am trying to do is:

1: Search for a folder named "hi" in "C:". If hi exists, enter it. If it doesnt, create it THEN enter it.
2: Inside hi, count FILES and see if there is greater or less than 44. If there is less than 44, create a file named "bye" inside
3: If there is more than 44, I should make a "hi2" in C. Repeat step 2 and 3 with hi2..(if greater than 44)...3....(if greater than 44)......4..... (if greater than 44).....5

I think the general picture is captured.

For now I have this:

 FTPClient f=new FTPClient();
        try 
        {
            f.connect("192.168.100.1");
            f.login("user","pass");
            f.changeWorkingDirectory("/");
            FTPFile[] list = f.listFiles();
            String cai="hi";
            int totalDIRS = 0;
            int totalFILES = 0;
            int doesfolderexist=0;

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

                    }
                    else
                    {   
                        f.makeDirectory(cai);
                        existcinternet=1;
                    }
                    System.out.println(list[i].getName());
                }
            }

For now, Im not sure where else to continue (as sad as it sounds)

Some hints?

Thank you

Recommended Answers

All 45 Replies

Your requirement is oddly worded... I assume that you are talking about adding "bye" file if there are less than 44 files...

Before going to the for-loop, outside the whole try-catch portion you posted, you need to create a flag and may name it as found, and then assign it to false. You also want to check whether or not the job is done by adding another flag named done and assign to false. Remember that found is corresponding to whether the specified directory exists and done is corresponding to the job is done. Also, create a counter and may assign as 1. The current code portion (besides the the declaration of FTPClient) of yours will be inside a while loop.

For the while-loop around the whole try, check its condition for not done. Inside it, if the counter is 1, the cai would be "hi" only. Else, the cai should be "hi"+counter (remember to convert it to string if there is any problem with concatenation).

In your for-loop, you must go through the whole list of the directory before you attempt to create a new directory. Go through the file list and check for directory only (you are doing it right now). If it is equal to the name, change the flag value (found) and break out of the loop. After the loop, check whether the flag is turned on. If it is, check for the number of files inside the directory; otherwise, create a new one and add the file. When check for the file number, if it is less than 44 files, add the bye file and turn the done value to true; otherwise, increment counter and let it go back to loop again.

Therefore, this whole process can be done in iteration instead of a recursion. You could do it in recursion, but you won't benefit from the recursive call because a solution of the problem does not need it -- you need to look inside only 1 directory/location.

Your requirement is oddly worded... I assume that you are talking about adding "bye" file if there are less than 44 files...

Before going to the for-loop, outside the whole try-catch portion you posted, you need to create a flag and may name it as found, and then assign it to false. You also want to check whether or not the job is done by adding another flag named done and assign to false. Remember that found is corresponding to whether the specified directory exists and done is corresponding to the job is done. Also, create a counter and may assign as 1. The current code portion (besides the the declaration of FTPClient) of yours will be inside a while loop.

For the while-loop around the whole try, check its condition for not done. Inside it, if the counter is 1, the cai would be "hi" only. Else, the cai should be "hi"+counter (remember to convert it to string if there is any problem with concatenation).

In your for-loop, you must go through the whole list of the directory before you attempt to create a new directory. Go through the file list and check for directory only (you are doing it right now). If it is equal to the name, change the flag value (found) and break out of the loop. After the loop, check whether the flag is turned on. If it is, check for the number of files inside the directory; otherwise, create a new one and add the file. When check for the file number, if it is less than 44 files, add the bye file and turn the done value to true; otherwise, increment counter and let it go back to loop again.

Therefore, this whole process can be done in iteration instead of a recursion. You could do it in recursion, but you won't benefit from the recursive call because a solution of the problem does not need it -- you need to look inside only 1 directory/location.

I did mean 44 files, sorry about that :(

Your solution with the "while-loop" sounds intresting and one I did not think of doing it. Thank you for the idea.

The thing is in this statement:

if (list[i].getName().equals(cai)) 
{
}

1: How do I "enter" directly into that directory (its path)?
2: How do I count the files inside that directory?

OK, I wrote some new code.....

Works somewhat. Creates the folder but doesnt put the file inside.

Will post in a min.

public static void main(String[] args) 
    {
        File e=new File("hi.txt");
        try {
            e.createNewFile();
        } catch (IOException e2) {

            e2.printStackTrace();
        }
        FileInputStream s=null;
        try {
            s=new FileInputStream(e);
        } catch (FileNotFoundException e1) {
            System.out.println("Cant find file: " + e1.getLocalizedMessage());
        }

        FTPClient f=new FTPClient();
        try 
        {
            System.out.println("Trying");
            f.connect("192.168.100.1");
            f.login("Anonymous","none");
            System.out.println("YES!");
            f.changeWorkingDirectory("/");
            FTPFile[] list = f.listFiles();
            String cai="folder";
            int found=0;
            int done=0;

            int totalDIRS = 0;
            int files=0;
            int i=0;
            int n=0;

            do{
            System.out.println("i is " + i);
                for (i=0;i<list.length;i++)
                {
                     System.out.println("i inside the for " + i);
                    if (list[i].getType() == FTPFile.DIRECTORY_TYPE)
                    {
                        System.out.println ("Folder detected:" + list[i].getName());        
                        if (list[i].getName().equals(cai) || list[i].getName().equals(cai+String.valueOf(i))) 
                            {     
                            System.out.println ("Found folder I must go inside of: " + list[i].getName());  
                                    if (i==0)
                                        {
                                            System.out.println ("Changing dir " + list[i].getName());
                                            f.changeWorkingDirectory("/"+cai);  
                                            found=1;
                                        }
                                        else
                                        {
                                            System.out.println ("Changing dir with "+ i + " I am in: " + list[i].getName());
                                            f.changeWorkingDirectory("/"+cai+String.valueOf(i));    
                                            found=1;
                                        }

                                    if (found==1)
                                    {
                                        FTPFile[] list2 = f.listFiles();
                                        System.out.println ("Counting the files inside");
                                        for (int x=0;x<list2.length;x++)
                                        {
                                            files=files+1;
                                        }


                                        if (files<44)
                                        {
                                            System.out.println ("inserting file");
                                            f.storeFile(f.printWorkingDirectory(), s);
                                            done=1;
                                        }
                                        else
                                        {

                                        }
                                    }
                        }
                        else
                        {   
                            System.out.println ("Cant find folder");
                            if ((found==0) && (done!=1))
                            {
                                System.out.println ("Making folder");
                                f.makeDirectory("/"+cai);
                                found=1;
                                i=0;

                            }
                            else if ((i>0) && (found==0) && (done!=1))
                            {
                                System.out.println ("Making the folder with " + i);
                                f.makeDirectory("/"+cai+String.valueOf(i)); 
                                found=1;
                                i=0;
                            }

                        }
                    }
                }
                found=0;
            }while (done==0);



            System.out.println ("Done");
            f.logout();
            f.disconnect();



        } 
        catch (SocketException n) 
        {
            System.out.println("Socket exception: " + n.getLocalizedMessage());
        } 
        catch (IOException n) 
        {
            System.out.println("IOException: " + n.getLocalizedMessage());
        }

    }

Basically thats what I have.

It doesnt count correctly the files when I go inside a folder (I believe it counts the file in / AKA C:/). It also doesnt put the file anwhere.

Line 56, you are not supposed to switch the flag because you didn't find the folder inside the directory you are looking at. Anyway, I will come back and give you a pseudo code for this.

OK, here is what I quickly wrote it for you...

counter <- 1
found <- false, is used to check whether the directory exists
done <- false, is used to check whether the work is done
list <- all files & folders in the C: directory
cai <- current looking directory name
while not done
  try
    if counter is equal to 1
      cai <- "hi"
    else
      cai <- "hi"+counter
    end if

    for each item i in the list
      if i is a directory and i is equal to cai
        found <- true
        break
      end if

      if found
        Go inside the directory and count number of files.
        Not sure whether or not listFiles() returns . and .., Check it!
        if total file is less than 44
          createFile(cai+"/"+whateverFileName)
          done <- true
        else if total file is equal to 44
          createFile(cai+"/bye.txt")
          done <- true
        else  # full
          increment counter by 1  // this will let the loop start again
                                  // with counter increased
        end if
      else    // not found
        create directory name cai
        createFile(cai+"/"+whateverFileName)
        done <- true
      end if
    end for
  catch
    handle the error
  end try/catch
end while

// better way is to implement a separate method for file creation,
// so that it would be easier for debugging and better readability
method createFile(filename)
  ...
end method

You should create another method to create a file for debugging & readibility purposes.

OK, new code coming up.....Hang on a minute :)

Testing it doesnt work correctly as I tried putting 45 seconds and it doesnt create a new folder. It doesnt insert anything at all but it should....

Here is the new code (based on what you put):

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

        File e=new File("test.txt");
            try 
            {

                e.createNewFile();
            } catch (IOException e2) {

                e2.printStackTrace();
            }
        FileInputStream s=null;
            try {
                s=new FileInputStream(e);
            } catch (FileNotFoundException e1) {
                System.out.println("error: " + e1.getLocalizedMessage());
            }

        FTPClient f=new FTPClient();

            System.out.println("Trying");
            f.connect("192.168.100.1");
            f.login("Anonymous","none");
            System.out.println("YES!");
            f.changeWorkingDirectory("/");
            FTPFile[] list = f.listFiles();
            String cai="folder";
            int found=0;
            int done=0;
            int counter=1;


            int files=0; //file counter

            int i=0;


            while (done!=1)
            {
                if (counter==1)
                {
                    cai="folder";
                }

                else
                {
                    cai="folder"+String.valueOf(counter);
                }

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

                    if (found==1)
                    {
                        f.changeWorkingDirectory("/"+cai);
                        FTPFile[] list2 = f.listFiles();
                        for (int x=0;x<list2.length;x++)
                        {
                            if (list2[x].getType() == FTPFile.FILE_TYPE)
                            {
                                files=files+1;
                            }

                        }
                        if (files<44)
                        {
                            f.storeFile(f.printWorkingDirectory(), s);
                        }
                        else
                        {
                            counter=counter+1;
                        }
                        files=0;    
                    }
                    else if (found==0)
                    {
                        f.makeDirectory("/"+cai);
                        f.storeFile(cai, s);
                        done=1;                 
                    }

                } //end for

            } //end while
        } //end main

Code only makes the first folder. Doesnt make/insert any file. When there is 45 files inside the folder, it doesnt do anything either.

OK some good news (not much but)

I have been able to count the files inside the folder. Less than 44, now it writes the file to the folder. If not, it doesnt do anything.

The next step would be since there are more than 44, it shoulds create a new folder and put it in there.

Will post new code shortly

Here is it:

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




        File e=new File("test.txt");
        FileInputStream s=null;
            try 
            {


                e.createNewFile();

            } catch (IOException e2) {

                e2.printStackTrace();
            }

            try {

                s=new FileInputStream(e);

            } catch (FileNotFoundException e1) {
                System.out.println("error: " + e1.getLocalizedMessage());
            }

        FTPClient f=new FTPClient();

            System.out.println("Trying");
            f.connect("192.168.100.2");
            f.login("Anonymous","none");
            System.out.println("YES!");
            f.changeWorkingDirectory("/");
            FTPFile[] list = f.listFiles();
            String cai="folder";
            int found=0;
            int done=0;
            int counter=1;


            int files=0;

            int i=0;


            while (done==0)
            {
                if (counter==1)
                {

                    cai="folder";
                }

                else
                {

                    cai="folder"+String.valueOf(counter);
                }

                for (i=0;i<list.length;i++)
                {

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

                        found=1;
                        //break; // WHAT IS THIS BREAKING EXACTLY??? THE IF OR THE FOR????
                    }

                    if (found==1)
                    {

                        f.changeWorkingDirectory("/"+cai);
                        FTPFile[] list2 = f.listFiles();
                        for (int x=0;x<list2.length;x++)
                        {

                            if (list2[x].getType() == FTPFile.FILE_TYPE)
                            {
                                files=files+1;

                            }

                        }
                        if (files<44)
                        {

                            f.storeFile(f.printWorkingDirectory()+"/test.txt", s);
                            done=1;
                        }
                        else
                        {

                            counter=counter+1;

                        }
                        files=0;

                    }
                    else if (found==0)
                    {

                        f.makeDirectory("/"+cai);
                        f.storeFile(cai, s);
                        done=1;                 
                    }

                } //end for

            } //end while
        } //end main

As you see Ive commented that break because Im not sure if that is really neccesary or what it is even breaking...

Oops, my pseudo code is wrapping incorrectly. >_< Sorry. I will need to try to implement it to ensure the modified version. Brb...

Oops, my pseudo code is wrapping incorrectly. >_< Sorry. I will need to try to implement it to ensure the modified version. Brb...

Thats OK :) Thank you for helping me out with this. Driving me crazy!

counter <- 1
found <- a found directory
done <- false, is used to check whether the work is done
list <- all files & folders in the C: directory
cai <- current looking directory name
while not done
  found <- null
  try
    if counter is equal to 1
      cai <- "hi"
    else
      cai <- "hi"+counter
    end if

    for each item i in the list
      if i is a directory and i is equal to cai
        found <- the found directory
        break
      end if
    end for

    if found is not null
      Go inside the directory and count number of files.
      Don't forget to skip if the File object is a directory -- isDirectory()
      if total file is less than 44
        createFile(cai+"/"+whateverFileName)
        done <- true
      else if total file is equal to 44
        createFile(cai+"/bye.txt")
        done <- true
      else  # full
        increment counter by 1  // this will let the loop start again
                                // with counter increased
      end if
    else    // not found
      create directory name cai
      createFile(cai+"/"+whateverFileName)
      done <- true
    end if
  catch
    handle the error
  end try/catch
end while

I have implemented it exactly the way this pseudo code does. I notice a couple points that may cause the problem you are encountering.

1) The file name must be different in order to create different file. One way to do is to pass in a number and append the number to the file name.
2) Permission denied may cause an exception that Java cannot create a file.
3) Do not reuse the list variable when you do file counting after you found a file. The reason is that if your folder is full, the same list would have already been changed.

I have implemented it exactly the way this pseudo code does. I notice a couple points that may cause the problem you are encountering. 1) The file name must be different in order to create different file. One way to do is to pass in a number and append the number to the file name.
2) Permission denied may cause an exception that Java cannot create a file.
3) Do not reuse the list variable when you do file counting after you found a file. The reason is that if your folder is full, the same list would have already been changed.

1) This wont be a problem as the filename will always be different (this is a section of a bigger program; im just testing it out with this)
2) Nonissue. No permissions on target
3) I do 2 lists; The one that runs thru C:/ and then another one that goes thru C:/thefolder . Im not sure if thats what you ment

Did you look at my latest code? Is your pseudocode based on it or your own personal way?

Yes, I looked at your latest code. You would see that the for-loop extends too far in the previous pseudo code. This time you will see that it is not. Also, I changed the found variable to be a File instead of a boolean in order to immediately deal with counting total file number. Also, it needs to be reset when the loop starts.

Yes, the pseudo code is just my solution style. You could implement it whatever way you like. ;)

found <- a found directory

What is this variable exactly? It sounded like a true (1)/false (0) variable but now it sounds like you ment it is something else...

Also, I changed the found variable to be a File instead of a boolean in order to immediately deal with counting total file number

OK this changes :P

BTW, would it be a "File" or a "FTPFile"?

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","none");
        System.out.println("YES!");
        File e=new File("test.txt");
        FileInputStream s=new FileInputStream(e);

        FTPFile[] list = f.listFiles();
        String cai="folder";
        while (done==0)
        {
            found=null;
            if (counter==1)
            {
                cai="folder";
            }
            else
            {
                cai="folder"+String.valueOf(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 files=0;
                for (int x=0;x<list2.length;x++)
                {
                    if (list2[x].getType()==FTPFile.FILE_TYPE)
                    {
                        files=files+1;
                    }
                }

                if (files<44)
                {
                    f.storeFile(f.printWorkingDirectory()+"/"+e.getName(), s);
                    done=1;
                }
                else
                {
                    counter=counter+1;
                }


            }//end if found not null

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

        }//end while



    } //end main

Doesnt create the file inside the folder.

Stupid code editor:

That like is

f.storeFile(f.printWorkingDirectory()+"/"+e.getName(),s);

That works. Let me test further.

OK, it still writes to the folder even if it has more than 44....

And if the folder does not exist, it makes the folder but doesnt put the file inside.

The problem seems to be the way you create file (in your storeFile()). Below is the code I implemented to test with file creation in the local storage.

/*
 The program will create files up to a given number of files. A folder named
 'hi' will be created if not exist. If the folder exists, a counter number
 will be appened to the folder name, i.e. hi2, hi3, etc. In the folder,
 there will be up to 45 files. Each file will be named 'hi'+numberOfFile+'.txt'
 if the total files is less than 44 files. The content of the file is 'hiya'.
 The 45th file will be named 'bye.txt' containing 'bye' as its content.
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.BufferedWriter;
import java.io.FileWriter;

class LocalFileLoopCreation {
  protected LocalFileLoopCreation() { }


  // Create files up to the given number.
  public static void createFiles(int num) {
    int fileNum = 1;
    while (fileNum<=num) {
      createNewFolderOrFile(fileNum);
      System.out.println("+Created file number "+fileNum);
      ++fileNum;
    }
  }  // createFiles(int)


  // Create a file named 'hi'+num+'.txt' in a folder named 'hi' if the total
  // file number in the folder is less than 44. If the folder does not exist,
  // create the folder first, and then add the file. If the total file number
  // is 44, create a file named 'bye'+num+'.txt' instead. Otherwise, loop until
  // it could add a file in a folder.
  private static void createNewFolderOrFile(int num) {
    int counter=1;
    boolean done=false;
    String cai="";
    File found;

    // getting the current folder's file list
    File f = new File(".");
    File[] list = f.listFiles();

    // loop through the directory and attempt to create a file/folder
    while (!done) {
      found = null;
      try {
        // set up a look-up folder name
        if (counter==1) { cai = "hi"; }
        else { cai = "hi"+Integer.toString(counter); }

        //  look for the folder
        for (int i=list.length-1; i>=0; i--) {
          if (list[i].isDirectory() && list[i].getName().equals(cai)) {
            found = list[i];  // remember it
            break;
          }
        }

        // dealing with found or not found the folder/directory
        if (found==null) {  // not found, create it
          File newDir = new File(f.getCanonicalPath()+"/"+cai);
          if (newDir.mkdirs()) {
            addAFile(newDir.getCanonicalPath()+"/hi"+Integer.toString(num)+".txt", "hiya");
            done = true;
            System.out.println("Created folder "+newDir.getCanonicalPath());
          }
          else {  // failed to create
            System.out.println("Cannot create folder "+f.getCanonicalPath()+"/"+cai);
            System.exit(1);
          }
        }
        else {  // found, count files
          File[] list2 = found.listFiles();
          int fileNumbers = 0;
          for (File lf : list2) {
            if (lf.isDirectory()) { continue; }
            ++fileNumbers;
          }
          if (fileNumbers<44) {  // add another file
            addAFile(found.getCanonicalPath()+"/hi"+Integer.toString(num)+".txt", "hiya");
            done = true;
          }
          else if (fileNumbers==44) {  // add 'bye' file
            addAFile(found.getCanonicalPath()+"/bye"+Integer.toString(num)+".txt", "bye");
            done = true;
          }
          else {  // let it loop
            System.out.println("Folder "+cai+" is full...");
            ++counter;
          }
        }
      }  // end try
      catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
      }
    }  // end while
  }  // createNewFolderOrFile()


  // Create a file using given path and name (filePandN) and the content.
  private static void addAFile(String filePandN, String content) {
    try {
      File f = new File(filePandN);
      BufferedWriter writer = new BufferedWriter(new FileWriter(f));
      writer.write(content);
    }
    catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }  // addAFile(String, String)


  public static void main (String[] args) {
    int totalFile = 100;
    if (args.length==1) {
      try { totalFile = Integer.parseInt(args[0]); }
      catch (Exception e) { totalFile = 100; }
    }
    // create 100 files in the current directory/folder
    LocalFileLoopCreation.createFiles(totalFile);
  }
}

PS: Line 41 will give you a problem. If there exists folder and it is full, what would happen in the next loop? Hint: Your current working folder is now changed to be subdirectory.

Thanks a lot. Im gone in 20 mins so Ill have to analyze all that code tommorow because implementing it now would take too long

getCanonicalPath()

Seen a lot of functions that file has but Ill have to check this one out and see what it does (and see if it works when there is a ftpfile involved)

As a personal request please do not use this:

for (File lf : list2) {


if (lf.isDirectory()) { continue; }
++fileNumbers;          }

I understand it but it is no clear.

Also, Im trying to translate all the "file" variables to "FTPFile" variables....So god knows what code is going out...

Im seeing problems with the code when it comes with FTP.

I used the Commons Net library:

http://commons.apache.org/net/

With these two objects: FTPClient and FTPFile.

http://commons.apache.org/net/api-3.1/org/apache/commons/net/ftp/FTPClient.html
http://commons.apache.org/net/api-3.1/org/apache/commons/net/ftp/FTPFile.html

I hardly will use the standard File Java object at all....

Could you please help me using FTP?

Thank you very much once again.

As a personal request please do not use this:

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.

// add this right before line 54
f.setFileTransferMode(FTP.ASCII_FILE_TYPE);
f.setFileType(FTP.ASCII_FILE_TYPE);

// add this line right before line 59
// you need to go backward 1 step to the parent directory (reset the variable)
f.changeToParentDirectory();
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.