Solved

Update file path

Posted on 2011-03-01
7
291 Views
Last Modified: 2012-08-13

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
Comment
Question by:AndyC1000
  • 3
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35013984
String inputdir2 = inputDir.replace("visualisation", "report");

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35013985

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

Expert Comment

by:objects
ID: 35013996
this is probably a little safer:

String inputdir2 = inputDir.replaceFirst("visualisation$", "report");
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:AndyC1000
ID: 35014013
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35014025
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
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 35014028
try this:

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

Assisted Solution

by:for_yan
for_yan earned 250 total points
ID: 35014096



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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now