0

I have few directorates and one file *.txt under one Directory A, Directory path is in QString

say c:/A/1/2/3/4 
    c:B/C/A/1/2/3/4/5/6

In my code I have only the full path, now I want to get the directory until A(name of A can change anytime) based on the file *.txt

inshort:- I want to parse all directory and get the directory until the place where *.txt present from right to left

Sijith
  • 3,740
  • 17
  • 61
  • 101
  • QDir dd(FilePath); QFileInfo pathFile(dd, "*.cnm"); QString FilePath1 = pathFile.path(); qDebug() << FilePath1; – Sijith Jul 21 '17 at 15:03
  • It is returning the same directory structure – Sijith Jul 21 '17 at 15:03
  • For clarity, I think you should add expected output to the question. What if there are more than one directories having name `A`, e.g. `c:/B/C/A/1/2/3/4/A/B/C`? Which one is the expected output, `c:/B/C/A/` or `c:/B/C/A/1/2/3/4/A/`? – putu Jul 22 '17 at 04:43

1 Answers1

0

Are you trying to get the string of the file path to A? Ex. If the file path was:

C:/Users/Admin/Desktop/file.txt

you want to get:

C:/Users

If so, all you have to do is find the second index location of "/". You could do something like this:

QString filePath = "C:/Users/Admin/Desktop/file.txt";
int index = filePath.indexOf("/");
index = filePath.indexOf("/",index+1);
QString shortenedPath = filePath.mid(0,index);

This should give you:

C:/Users/

If that isn't what you meant, then sorry for the long response.

lrmlrm97
  • 71
  • 4
  • I think in this case `A` in question is equal to `Desktop` in your answer. So, what the OP wants is to extract `C:/Users/Admin/Desktop/1/2/3` to `C:/Users/Admin/Desktop/`, `C:/Users/Admin/Desktop/X/Y/Z` to `C:/Users/Admin/Desktop/`... – putu Jul 22 '17 at 04:38
  • THanks for giving your time to give response – Sijith Jul 22 '17 at 16:40