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.