【问题标题】:Delphi - Format Filename from FTP [duplicate]Delphi - 从 FTP 格式化文件名 [重复]
【发布时间】:2013-05-20 12:37:49
【问题描述】:

好的,我有以下问题:

我喜欢在我的 FTP 服务器上的一个特殊文件夹中获取所有 .txt 文件的列表。 我已经解决了。

 if IdFTP1.DirectoryListing.Count>0 then
          for i := 2 to IdFTP1.DirectoryListing.Count - 1 do
              with ListViewMain.Items.Add do
                Caption:=idftp1.DirectoryListing.Items[i].FileName;

但是现在,我的 .txt 文件是这样的:

项目名称###日期###status.txt

我喜欢在加载到 ListView 之前格式化文件名。在### 之后,字符串应该进入一个子项。 所以最后它应该是这样的:

ListView Item = Projectname
ListView Subitem1 = Date
ListView SubItem2 = status

我该怎么做?

【问题讨论】:

  • 可以使用 Copy 和 Pos 甚至更好的 PosEx 来完成,google 应该很容易找到解决方案

标签: string delphi listview split format


【解决方案1】:

只需使用PosCopy 将部分分离为单独的部分,然后根据需要添加它们:

编辑:修复了循环内对i 的无意分配(由复制/粘贴然后将DirectoryListing 循环从问题添加到我的答案)。声明了新的 j 变量以用于对字符串进行索引。

var
  i, j: Integer;
  ProjName, ProjDate, ProjStatus: string;
  Temp: string;
begin
  // Other code here...
  for i := 2 to IdFTP1.DirectoryListing.Count - 1 do
  begin
    Temp := IdFTP1.DirectoryListing.Items[i].FileName;
    j := Pos('###', Temp);
    ProjName := Copy(Temp, 1, j - 1);
    System.Delete(Temp, 1, j + 2);    // Remove ProjName and three # chars
    j := Pos('###', Temp);
    ProjDate := Copy(Temp, 1, j - 1); // Grab project date
    ProjStatus := Copy(Temp, j + 3, Length(Temp));  // Grab status
    ProjStatus := ChangeFileExt(ProjStatus, '');   // Remove extension from filename
    with ListViewMain.Items.Add do
    begin
      Caption := ProjName;
      SubItems.Add(ProjDate);
      SubItems.Add(ProjStatus);
    end;
  end;
end;

根据您的 Delphi 版本,您可以使用 PosEx 来消除对 System.Delete 的调用,但除非您处理大量文件或运行数百万次,否则这样做可能没问题在这里。

【讨论】:

  • 我是 Delphi XE2 架构师。 i := Pos('###', Temp);在这一行 Delphi 给我一个错误... > > C:\Users\User\Desktop\MyProject\Client\Rev 0.2 - > Kopie\Unit1.pas(153,7): error E2081: E2081 Anweisung für > FOR- Schleifenvariablen 'i'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 1970-01-01
  • 2012-02-07
  • 1970-01-01
  • 2017-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多