0

okay, I've the following problem:

I like to get a list of all .txt files on my FTP Server in a special folder. I solved this already.

 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;

But now, my .txt Files are like this:

Projectname###Date###status.txt

I like to format the filename before loading into the ListView. After the ###'s the string should come into a Subitem. So in the end it should look like this:

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

How can I do this?

Madara Uchiha
  • 127
  • 2
  • 11

1 Answers1

0

Just use Pos and Copy to separate out the parts into separate parts, and then add them as you'd like:

EDIT: Fixed unintentional assignment to i within the loop (caused by copy/paste and then adding the DirectoryListing loop from the question to my answer). Declared new j variable to use for indexing into the strings.

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;

Depending on your Delphi version, you can use PosEx to eliminate the call to System.Delete, but unless you're dealing with a lot of files or running this millions of times, it's probably fine to do the way it is here.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • I've Delphi XE2 architect. i := Pos('###', Temp); In this line Delphi give's me an error... > > C:\Users\User\Desktop\MyProject\Client\Rev 0.2 - > Kopie\Unit1.pas(153,7): error E2081: E2081 Anweisung für > FOR-Schleifenvariablen 'i' – Madara Uchiha May 20 '13 at 13:42