复制/提取文件路径的一部分?

发布于 2024-11-13 08:22:31 字数 489 浏览 2 评论 0原文

如何复制/提取文件路径的一部分?

例如,假设我有以下路径:D:\Programs\Tools\Bin\Somefile.dat

我如何复制/提取它以使其像这样:

C:\Users\ Bin\Somefile.dat

C:\Users\Tools\Bin\Somefile.dat

C:\Users\Programs\Tools\Bin\Somefile.dat >

请注意,上面的示例将原始路径的一部分更改为另一个目录。我认为这可能称为扩展名称或其他什么?

PS,我已经知道 ExtractFileName 和 ExtractFilePath 等,无论如何,路径都可以是动态的,因为它不会是硬编码路径,而是不断变化的,所以这些函数可能不好。

谢谢。

How can I copy/extract part of a File path?

For example, say if I have this path: D:\Programs\Tools\Bin\Somefile.dat

how could I copy/extract it to make it like this:

C:\Users\Bin\Somefile.dat

or

C:\Users\Tools\Bin\Somefile.dat

or

C:\Users\Programs\Tools\Bin\Somefile.dat

Notice that the examples above are taking part of the original path, and changing it to another directory. I think this is called Expand name or something maybe??

PS, I already know about ExtractFileName and ExtractFilePath etc, the path anyway could be dynamic in that it wont be a hard coded path, but ever changing, so these functions are likely no good.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩心态 2024-11-20 08:22:46

尝试使用 PathAppend 和 PathExtractElements 函数

try using the PathAppend and PathExtractElements functions

临走之时 2024-11-20 08:22:43

您看过 ExtractFileName 函数吗?一切都为您内置。当然,根据您的路径/文件的来源,您可能需要 ExtractFilePath 或其他相关函数。

Have you looked at the ExtractFileName function? all built in for you. Depending on where your paths/files are coming from of course, you may need the ExtractFilePath, or other related functions.

你爱我像她 2024-11-20 08:22:40

这是一个快速实现,它返回路径的尾部,包括指定数量的元素。还有一些如何使用它的演示,结果正是您所要求的。不幸的是,我不完全理解您所追求的转换:这可能正是您所追求的,或者可能是完全错误的,这恰好产生了看起来像您的示例的结果:

program Project25;

{$APPTYPE CONSOLE}

uses
  SysUtils;

function ExtractPathTail(const OriginalPath:string; const PathElemCount:Integer):string;
var i, start, found_delimiters: Integer;
begin
  start := 0;
  found_delimiters := 0;
  for i:=Length(OriginalPath) downto 1 do
    if OriginalPath[i] = '\' then
    begin
      Inc(found_delimiters);
      if found_delimiters = PathElemCount then
      begin
        start := i;
        Break;
      end;
    end;
  if start = 0 then
    raise Exception.Create('Original path is too short, unable to cut enough elements from the tail.') // mangled English to help SO's code formatter
  else
    Result := System.Copy(OriginalPath, start+1, MaxInt);
end;

const SamplePath = 'D:\Programs\Tools\Bin\Somefile.dat';

begin
  try
    WriteLn('C:\Users\' + ExtractPathTail(SamplePath, 2)); // prints: C:\Users\Bin\Somefile.dat
    WriteLn('C:\Users\' + ExtractPathTail(SamplePath, 3)); // prints: C:\Users\Tools\Bin\Somefile.dat
    WriteLn('C:\Users\Programs\' + ExtractPathTail(SamplePath, 3)); // prints: C:\Users\Programs\Tools\Bin\Somefile.dat
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Here's a quick implementation that returns the TAIL of a path, including the specified number of elements. There's also a bit of demo of how to use it, and the results are exactly the ones you requested. Unfortunately I don't fully understand what transformations you're after: this might be exactly what you're after, or it might be something entirely wrong, that just happens to produce a result that looks like your sample:

program Project25;

{$APPTYPE CONSOLE}

uses
  SysUtils;

function ExtractPathTail(const OriginalPath:string; const PathElemCount:Integer):string;
var i, start, found_delimiters: Integer;
begin
  start := 0;
  found_delimiters := 0;
  for i:=Length(OriginalPath) downto 1 do
    if OriginalPath[i] = '\' then
    begin
      Inc(found_delimiters);
      if found_delimiters = PathElemCount then
      begin
        start := i;
        Break;
      end;
    end;
  if start = 0 then
    raise Exception.Create('Original path is too short, unable to cut enough elements from the tail.') // mangled English to help SO's code formatter
  else
    Result := System.Copy(OriginalPath, start+1, MaxInt);
end;

const SamplePath = 'D:\Programs\Tools\Bin\Somefile.dat';

begin
  try
    WriteLn('C:\Users\' + ExtractPathTail(SamplePath, 2)); // prints: C:\Users\Bin\Somefile.dat
    WriteLn('C:\Users\' + ExtractPathTail(SamplePath, 3)); // prints: C:\Users\Tools\Bin\Somefile.dat
    WriteLn('C:\Users\Programs\' + ExtractPathTail(SamplePath, 3)); // prints: C:\Users\Programs\Tools\Bin\Somefile.dat
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文