Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

Update file path


Hello,

I want to know if its possible to use dos commands in strings when specifying a file path.

The user to specifies an input directory.

I want to be able to switch the directory string in the code.

String inputdir = "data/results/visualisation";
to
String inputdir2 = "data/results/report";

I know that the report folder will always live in the same level as the inputdir visualisation.

I've tried alot of combinations nothing is working.

0
AndyC1000
Asked:
AndyC1000
  • 3
  • 3
2 Solutions
 
objectsCommented:
String inputdir2 = inputDir.replace("visualisation", "report");

0
 
for_yanCommented:

Not sure I understand what is your question - maybe this is what you want:
 
inputdir2 = (new File(inputdir)).getParentPath() + File.separator + "report"
0
 
objectsCommented:
this is probably a little safer:

String inputdir2 = inputDir.replaceFirst("visualisation$", "report");
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AndyC1000Author Commented:
The program gets the first path from an argument.

I wont be able to define what do replace other than knowing that Report must be inserted into the same level as Visualisation (which could be abc etc).

I've tried but it doesn't work

inputdir += "\\..\\Report";

Open in new window

0
 
for_yanCommented:
So you mean that they not necessary would be at the end of the path? it may be

data/results/visualistaion/abc  --> data/results/report/abc  
0
 
objectsCommented:
try this:

String inputDir2 = new File(new File(inputDir).getParentFile(), "report").getPath();
0
 
for_yanCommented:



If visualistaion and report could be in the middle of the path,
then you want to split with something like that:

 String[] subDirs = path.split(Pattern.quote(File.separator));

String s = 0;
for(int j=0; j<subDirs.length; j++){
if(subDirs[j].equals("visualisation"))s +=  File.separator + "results";
else s +=  File.separator  + subDirs[j];
}


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now