简体   繁体   English

Qt根据目录内容获取文件目录

[英]Qt get File directory based on directory content

I have few directorates and one file *.txt under one Directory A, Directory path is in QString 我在一个目录A下的目录很少,只有一个文件* .txt,目录路径在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 在我的代码中,我只有完整的路径,现在我想获取目录,直到基于文件* .txt的A(A的名称可以随时更改)。

inshort:- I want to parse all directory and get the directory until the place where *.txt present from right to left 简而言之:-我想解析所有目录并获取目录,直到从右到左显示* .txt的地方

Are you trying to get the string of the file path to A? 您是否正在尝试获取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. 如果这不是您的意思,请您多加回应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM