我有一个保存为字符串的目录的路径,想知道如何从这个字符串中轻松而健壮地提取父目录?
我试图在FileUtil和SysUtils中查看是否有一些方法,但到目前为止还没有找到任何方法。
答案 0 :(得分:2)
更简单的方法是:
SELECT issues.*, budget_items.description, contracts.completed_at AS resolved_at
FROM issues
LEFT JOIN budget_items ON issues.id = budget_items.issue_id
LEFT JOIN contracts ON budget_items.id = contracts.budget_item_id
WHERE issues.base_item_id = 6
GROUP BY issues.id
ORDER BY contracts.completed_at DESC
LIMIT 1
这适用于三个主要平台(Linux,Mac OS X和Windows),而thePath可能会引用文件或文件夹。
答案 1 :(得分:1)
最简单的方法是找到最后一个路径分隔符并修剪源字符串。 BTW有一些替代方案:
program Project1;
uses
sysutils;
var
sExe: string;
sParent: string;
sParentProper: string;
begin
sExe := ExtractFilePath(ParamStr(0)); // Get executable directory
Writeln(sExe);
sParent := IncludeTrailingPathDelimiter(sExe) + '..' + PathDelim; // Make parent path for executable
Writeln(sParent);
sParentProper := ExpandFileName(sParent); // Get absolute path based on relative path
WriteLn(sParentProper);
Readln;
end.
输出是:
C:\Users\nd\AppData\Local\Temp\
C:\Users\nd\AppData\Local\Temp\..\
C:\Users\nd\AppData\Local\
因此,使用此技术的正确方法是ExpandFileName(IncludeTrailingPathDelimiter(sBasePath) + '..')
PS:我们只使用sysutils
单位,因此它是纯FPC解决方案,不需要任何LCL库。
答案 2 :(得分:0)
嗯,我当然只是在这里询问之后就把它想出来了:
parentDirPath := FileUtil.ExtractFileNameOnly(
FileUtil.ChompPathDelim(theSubDirPath));
......会的!
FileUtil.ChompPathDelim()
部分是“欺骗”FPC以确认顶级文件夹是“文件”(因此不允许使用尾部斜线)所必需的。