-3

I am creating a program that among others needs to access files inside directories. I would like the user to have the ability to pass as many parameters(i.e. directories) as they like, for example myFile.jar dir1/subDir dir2 file.txt

I would like to have the ability to allow the user to go in the sub dir as well, but for only the directories they want. so for using subdirectories of dir2 but not dir1/subDir i was thinking something like this: myFile.jar dir1.subDir dir2 -s file.txt

Where -s would stand for go into the subdirs of this directory only. how can i implement this? start looking on the args array for the "-s" string and use that where available? Is that the correct way of doing?

Edit/Update:

I am editing this old post, just to make sure that i what i did is right. It gets the job done, i am just not sure if it is anywhere near to a professional level of programming

This is my main method where i handle my passed arguments

public static void main(String [ ] args){
    boolean subfolder=false;
    if (args.length<1){
        System.out.println("at least one file must be provided");
        System.out.println("Usage:\n [option] {folders} [option] {folders} {files}");
        System.out.println("Options\n\t -s will visit all subfolders under the folder(s) provided until a -n is found");
        System.out.println("\t -n (default)will Not visit subfolders for the folder(s) provided until a -s is found");
    }
    else
    for (int i=0;i<args.length;i++){
        if((args[i].length()==2)&&(args[i].contains("-"))){
//          this is an options args
            if(args[i].contains("s")){
                subfolder=true;
            }else 
            subfolder=false;
        }
        else{
            if(new File(args[i]).isFile()){
//              this is a file
           //do what i need to do with this file
        }
        else if(new File(args[i]).isDirectory()){
//      this is a directory get the files in that directory and if subfolders=true, then get the files inside the subfolders
            getSubDirs(new File(args[i]),subfolder);
            }
            else
            {
            System.out.println("at least one file must be provided");
            System.out.println("Usage:\n [option] {folders} [option] {folders} {files}");
            System.out.println("Options\n\t -s will visit all subfolders under the folder(s) provided until a -n is found");            System.out.println("\t -n (default)will Not visit subfolders for the folder(s) provided until a -s is found");
            }
        }//else
    }//for
}//method

While executed with out any arguments, or wrong arguments i get this

at least one file must be provided
Usage:
 [option] {folders} [option] {folders} {files}
Options
     -s will visit all subfolders under the folder(s) provided until a -n is found
     -n (default)will Not visit subfolders for the folder(s) provided until a -s is found

and while using this

-s /home/ilias/Programming/Tomcat /home/ilias/EclipseWorkspace/test -n /home/ilias/Programming/Eclipse/eclipse /home/ilias/EclipseWorkspace/Servers

I get a list of all the files in all the subfolders under Tomcat and under test and only the files under Eclipse/eclipse and underServers with out the subfolders.

Skaros Ilias
  • 1,008
  • 12
  • 40

1 Answers1

0

from your second example String[] args in main gets filled in by splitting the input on space characters.

public static void main(String[] args){
    args[0] // myFile.jar
    args[1] // dir1.subDir
    args[2] // dir2
    args[3] // -s
    args[4] // file.txt
}

You have to build in the functionality yourself. I suggest having your first params be switches (prefixed with - like -s) Then you can accept a bunch of strings. You can pass them into a function to check if they are files

File f = new File(filePathString);
if(f.exists() && !f.isDirectory()) { /* do something */ }

Source for second snippet

From here you need to identify if a file is a directory. If it is call list() on the directory to get its child files/directories and repeat the process.

Community
  • 1
  • 1
t3dodson
  • 3,949
  • 2
  • 29
  • 40
  • i am not asking how to implement the program to read the files and directories, sorry if i was unclear. I want to know if calling the program in such a matter is a good tactic, by reading the prefix and deciding if it needs a subdir crawling or not – Skaros Ilias May 21 '15 at 23:35
  • @SkarosIlias yes this is fine and is how a lot of program works. – t3dodson May 21 '15 at 23:36