Avatar billede gozar Nybegynder
20. august 2000 - 16:16 Der er 10 kommentarer og
2 løsninger

Omskrivning af kode!!

Hvordan omskriver jeg denne kode, til at kunde liste roden fra en ftp server??

Jeg bruger komandoen FTP1.List

Og OnListItem i NMFTP skal koden, som viser rod filerne i en ftp dir.

Har ingen ide om hvordan dette gøres!!

  -- Kode start --

Var
  Fsr : TSearchRec;
  SogSti : String;
  ListItem: TListItem;
  Sti : String;
Begin
  Sti := (\'c:\\\');
  If Sti[Length(Sti)] = \'\\\' then else
    Sti := Sti + \'\\\';
  If LastDelimiter(\'g\',Sti) > 0 then else
    SogSti := Sti + \'*.*\';

  If FindFirst(SogSti, faAnyFile, Fsr) = 0 then
    If FSr.Attr <> faDirectory then
      Begin
        ListItem := Server.Items.Add;
        ListItem.Caption := Fsr.Name;
        ListItem.SubItems.Add(ExtractFileExt(Fsr.Name)); //Denne linie tilføjer efternavnet i den næste celle.
        ListItem.SubItems.Add(IntToStr(Fsr.Size));
      end;

  While FindNext(Fsr) = 0 do
    If FSr.Attr <> faDirectory then
      Begin
        ListItem := Server.Items.Add;
        ListItem.Caption := Fsr.Name;
        ListItem.SubItems.Add(ExtractFileExt(Fsr.Name)); //Denne linie tilføjer efternavnet i den næste celle.
        ListItem.SubItems.Add(IntToStr(Fsr.Size));
      end;
  FindClose(Fsr);
end;

  -- Kode Slut --
Avatar billede snowball Novice
20. august 2000 - 16:47 #1
I Delphi\'s hjælp er der et rigtig godt eksempel på hvordan man bruger FTP komponenten !


Det er nok dette stykke kode du skal bruge:

case Trans_Type of
    cmdList:
      begin
        for I := 0 to (StringGrid1.ColCount - 1) do

          StringGrid1.Cols[I].Clear;
        StringGrid1.RowCount := NMFTP1.FTPDirectoryList.name.Count;
        StringGrid1.ColCount := 4;
        StringGrid1.Cells[0, 0] := \'Filename\';
        StringGrid1.Cells[1, 0] := \'File Size\';
        StringGrid1.Cells[2, 0] := \'Modified Date\';
        StringGrid1.Cells[3, 0] := \'Attributes\';
        for I := 0 to (NMFTP1.FTPDirectoryList.name.Count - 1) do
          with NMFTP1.FTPDirectoryList do
            begin
              StringGrid1.Cells[0, I+1] := name[I];

              StringGrid1.Cells[1, I+1] := Size[I];
              StringGrid1.Cells[2, I+1] := ModifDate[I];
              StringGrid1.Cells[3, I+1] := Attribute[I];
            end;
      end;

Snowball
Avatar billede gozar Nybegynder
20. august 2000 - 18:03 #2
Jeg får nu bare at vide hjælpe filen ikke findes, hvor efter der bliver åbnet en hjælpe fil!!!!!
Avatar billede gozar Nybegynder
20. august 2000 - 18:07 #3
Jeg får allerede fejl ved den første linie!!
Avatar billede snowball Novice
20. august 2000 - 18:17 #4
Her er hele eksemplet:

Example

To recreate this example, you will need to create a new blank Delphi application.

Place 3 TEdits, 9 TButtons, 2 TMemos, a TStatusBar, a TRadioGroup, and a TNMFTP on the form.

Component Descriptions:

Edit1: Host
Edit2: User ID
Edit3: Password
Button1: Connect/Disconnect
Button2: NList
Button3: ChangeDir
Button4: Download
Button5: Upload
Button6: UploadAppend
Button7: UploadUnique
Button8: Upload Restore
Button9: Abort
Memo1: Status Window
Memo2: Directory List display
RadioGroup1: Transfer Mode *
StatusBar1: File Transfer progress

* Change RadioGroup1.Items to MODE_ASCCI, MODE_IMAGE, and MODE_BYTE, in that order.

Insert the following code into Button1\'s OnClick event:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if NMFTP1.Connected then
    NMFTP1.Disconnect
  else
    begin
      NMFTP1.Host := Edit1.Text;
      NMFTP1.UserID := Edit2.Text;
      NMFTP1.Password := Edit3.Text;
      NMFTP1.Connect;
    end;
end;

When Button1 is clicked, if the Connected property is TRUE, a connection is present, and the Disconnect method is called to disconnect from the remote FTP host. If Connected is FALSE, there is no connection present, so the Host property is set to the value in Edit1 to specify the remote host to connect to. The UserID property is set to the value in Edit2, and the Password property is set to the value in Edit3. The UserID and Password are used to log in to the remote host when connected.

Insert the following code into Button2\'s OnClick event:

procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo2.Clear;
  NMFTP1.NList;
end;

When Button2 is clicked, any text being displayed in Memo2 is cleared. The NList method is called, retrieving the names of all files and directories in the current working directory. The results of this method call can be seen in the OnListItem event.


Insert the following code into Button3\'s OnClick event:

procedure TForm1.Button3Click(Sender: TObject);
var
  TheDir: String;
begin
  if InputQuery(\'Change directory\', \'Directory name?\', TheDir) then

    NMFTP1.ChangeDir(TheDir);
end;

When Button3 is clicked, the InputQuery function is used to obtain the name of the directory desired. If the Ok button is clicked, the ChangeDir method is used to change the current working directory to the directory specified.


Insert the following code into Button4\'s OnClick event:

procedure TForm1.Button4Click(Sender: TObject);
var
  RemoteFile,
  LocalFile: String;
  O: TOpenDialog;
begin
  if InputQuery(\'Download a file\', \'File to download: \', RemoteFile) then
    begin
      O := TOpenDialog.Create(Self);
      try
        O.Title := \'Save file as\';
        if O.Execute then

          begin
            LocalFile := O.FileName;
            case RadioGroup1.ItemIndex of
              0: NMFTP1.Mode(MODE_ASCII);
              1: NMFTP1.Mode(MODE_IMAGE);
              2: NMFTP1.Mode(MODE_BYTE);
            end;
            NMFTP1.Download(RemoteFile, LocalFile);
          end;
      finally
        O.Free;
      end;
    end;
end;

When Button4 is clicked, the InputQuery function is used to retrieve the name of a remote file to download to the local computer. if the Ok button is clicked, an OpenDialog is used to retrieve the name to store the file as on the local computer. If the Open button is clicked, the Mode method is called, changing the transfer mode depending on the selected item in RadioGroup1. Then the Download method is used to download the remote file to the local disk.

Insert the following code into NMFTP1\'s OnDisconnect event:

procedure TForm1.NMFTP1Disconnect(Sender: TObject);
begin
  Memo1.Lines.Add(\'Disconnected\');
  Button1.Caption := \'Connect\';
end;

When the local computer disconnects from the remote host, the OnDisconnect event is called. In this instance, the status display (Memo1) is updated to display the disconnect status, and the caption of Button1 is changed to Connect (Since Button1 is used for connecting and disconnecting).


Insert the following code into NMFTP1\'s OnPacketRecvd event:

procedure TForm1.NMFTP1PacketRecvd(Sender: TObject);
begin
  StatusBar1.SimpleText := IntToStr(NMFTP1.BytesRecvd)+\' bytes received out of \'+IntToStr(NMFTP1.BytesTotal);

end;

When data is received from the remote host during a file transfer, the OnPacketRecvd event is called. In this instance, StatusBar1 displays the progress of the transfer by displaying the BytesRecvd property, which contains the number of bytes received, and the BytesTotal property, which contains the total number of bytes to transfer.


Insert the following code into NMFTP1\'s OnPacketSent event:

procedure TForm1.NMFTP1PacketSent(Sender: TObject);
begin
  StatusBar1.SimpleText := IntToStr(NMFTP1.BytesSent)+\' bytes sent out of \'+IntToStr(NMFTP1.BytesTotal);
end;

When data is sent to the remote host during a file transfer, the OnPacketSent event is called. In this instance, StatusBar1 displays the progress of the transfer by displaying the BytesSent property, which contains the number of bytes sent, and the BytesTotal property, which contains the total number of bytes to transfer.

Insert the following code into NMFTP1\'s OnTransactionStart event:

procedure TForm1.NMFTP1TransactionStart(Sender: TObject);
begin
  Memo1.Lines.Add(\'Transaction Start\');
end;

When a data transfer begins, the OnTransactionStart event is called. In this instance, the Status Memo, Memo1, displays that the transaction started.


Insert the following code into NMFTP1\'s OnTransactionStop event:

procedure TForm1.NMFTP1TransactionStop(Sender: TObject);
begin
  Memo1.Lines.Add(\'Transaction Stop\');
end;

When a data transfer completes, the OnTransactionStop event is called. In this instance, the Status Memo, Memo1, displays that the transaction has finished.



Insert the following code into Button5\'s OnClick event:

procedure TForm1.Button5Click(Sender: TObject);
var
  LocalFile,
  RemoteFile: String;
  F: File of Byte;
  FSize: Integer;
  O: TOpenDialog;
begin
  O := TOpenDialog.Create(Self);
  try
    O.Title := \'Select file to upload\';
    if O.Execute then
      if InputQuery(\'Choose Remote File Name\', \'Filename?\', RemoteFile) then
        begin
          LocalFile := O.FileName;
          case RadioGroup1.ItemIndex of
            0: NMFTP1.Mode(MODE_ASCII);
            1: NMFTP1.Mode(MODE_IMAGE);
            2: NMFTP1.Mode(MODE_BYTE);
          end;
          AssignFile(F, LocalFile);
          Reset(F);
          FSize := FileSize(F);
          CloseFile(F);
          NMFTP1.Allocate(FSize);

          NMFTP1.Upload(LocalFile, RemoteFile);
        end;
  finally
    O.Free;
  end;
end;

When Button5 is clicked, an Open Dialog is displayed to select a file on the local computer to upload to the remote host. If the Open button is clicked, the InputQuery function is used to retrieve the name the file will be stored as on the remote host. If the Ok button is clicked, the file is opened so it\'s size can be determined. The Allocate method is called to reserve space on the remote host for the file. This is not normally necessary, but is done here to illustrate the use of the method. The Upload method is used to send the file to the remote host.

Insert the following code into Button6\'s OnClick event:

procedure TForm1.Button6Click(Sender: TObject);
var
  LocalFile,
  RemoteFile: String;
  O: TOpenDialog;
begin
  O := TOpenDialog.Create(Self);
  try
    O.Title := \'Select file to upload\';
    if O.Execute then
      if InputQuery(\'Choose Remote File Name\', \'Filename?\', RemoteFile) then
        begin
          LocalFile := O.FileName;
          case RadioGroup1.ItemIndex of
            0: NMFTP1.Mode(MODE_ASCII);
            1: NMFTP1.Mode(MODE_IMAGE);
            2: NMFTP1.Mode(MODE_BYTE);
          end;
          NMFTP1.UploadAppend(LocalFile, RemoteFile);
        end;
  finally
    O.Free;
  end;
end;

When Button6 is clicked, an Open Dialog is displayed to select a file on the local computer to upload to the remote host. If the Open button is clicked, the InputQuery function is used to retrieve the name the file will be stored as on the remote host. If the Ok button is clicked, the UploadAppend method is used to send the file to the remote host. If a file with the same name already exists on the remote host, the file being sent is appended to the end of the existing file.

Insert the following code into Button7\'s OnClick event:

procedure TForm1.Button7Click(Sender: TObject);
var
  LocalFile: String;
  O: TOpenDialog;
begin
  O := TOpenDialog.Create(Self);
  try
    O.Title := \'Upload file\';
    if O.Execute then
      begin
        LocalFile := O.FileName;
        NMFTP1.UploadUnique(LocalFile);
      end;
  finally
    O.Free;
  end;
end;

When Button7 is clicked, an Open Dialog is displayed to select the file on the local computer to upload to the remote host. If the Open button is clicked, the UploadUnique method is used to store the file on the remote FTP host with a unique filename given by the FTP host.

Insert the following code into NMFTP1\'s OnFailure event:

procedure TForm1.NMFTP1Failure(var Handled: Boolean; Trans_Type: TCmdType);
begin
  case Trans_Type of
    cmdChangeDir: Memo1.Lines.Add(\'ChangeDir failure\');
    cmdMakeDir: Memo1.Lines.Add(\'MakeDir failure\');
    cmdDelete: Memo1.Lines.Add(\'Delete failure\');
    cmdRemoveDir: Memo1.Lines.Add(\'RemoveDir failure\');
    cmdList: Memo1.Lines.Add(\'List failure\');
    cmdRename: Memo1.Lines.Add(\'Rename failure\');
    cmdUpRestore: Memo1.Lines.Add(\'UploadRestore failure\');
    cmdDownRestore: Memo1.Lines.Add(\'DownloadRestore failure\');
    cmdDownload: Memo1.Lines.Add(\'Download failure\');

    cmdUpload: Memo1.Lines.Add(\'Upload failure\');
    cmdAppend: Memo1.Lines.Add(\'UploadAppend failure\');
    cmdReInit: Memo1.Lines.Add(\'Reinitialize failure\');
    cmdAllocate: Memo1.Lines.Add(\'Allocate failure\');
    cmdNList: Memo1.Lines.Add(\'NList failure\');
    cmdDoCommand: Memo1.Lines.Add(\'DoCommand failure\');
    cmdCurrentDir: Memo1.Lines.Add(\'CurrentDir failure\');
  end;
end;

When an FTP command fails, the OnFailure event is called. In this case, the Handled parameter is left the default (FALSE), so an exception will be raised, in addition to a failure notification being added to the status display (Memo1) noting the command that failed.

Insert the following code into NMFTP1\'s OnSuccess event:

procedure TForm1.NMFTP1Success(Trans_Type: TCmdType);
begin
  case Trans_Type of
    cmdChangeDir: Memo1.Lines.Add(\'ChangeDir success\');
    cmdMakeDir: Memo1.Lines.Add(\'MakeDir success\');
    cmdDelete: Memo1.Lines.Add(\'Delete success\');
    cmdRemoveDir: Memo1.Lines.Add(\'RemoveDir success\');
    cmdList: Memo1.Lines.Add(\'List success\');
    cmdRename: Memo1.Lines.Add(\'Rename success\');
    cmdUpRestore: Memo1.Lines.Add(\'UploadRestore success\');
    cmdDownRestore: Memo1.Lines.Add(\'DownloadRestore success\');
    cmdDownload: Memo1.Lines.Add(\'Download success\');

    cmdUpload: Memo1.Lines.Add(\'Upload success\');
    cmdAppend: Memo1.Lines.Add(\'UploadAppend success\');
    cmdReInit: Memo1.Lines.Add(\'Reinitialize success\');
    cmdAllocate: Memo1.Lines.Add(\'Allocate success\');
    cmdNList: Memo1.Lines.Add(\'NList success\');
    cmdDoCommand: Memo1.Lines.Add(\'DoCommand success\');
    cmdCurrentDir: Memo1.Lines.Add(\'CurrentDir success\');
  end;
end;

When an FTP command succeeds, the OnSuccess event is called. In this instance, the command that succeeded is displayed in the status display (Memo1).

Insert the following code into Button8\'s OnClick event:

procedure TForm1.Button8Click(Sender: TObject);
var
  LocalFile,
  RemoteFile: String;
  FPosition: Integer;
  FPos: String;
  O: TOpenDialog;
begin
  O := TOpenDialog.Create(Self);
  try
    O.Title := \'Select file to upload\';
    if O.Execute then
      if InputQuery(\'Choose Remote File Name\', \'Filename?\', RemoteFile) then
        if InputQuery(\'Choose restoration point\', \'Byte Count: \', FPos) then
          begin
            FPosition := StrToInt(Fpos);
            LocalFile := O.FileName;
            case RadioGroup1.ItemIndex of
              0: NMFTP1.Mode(MODE_ASCII);
              1: NMFTP1.Mode(MODE_IMAGE);
              2: NMFTP1.Mode(MODE_BYTE);

            end;
            NMFTP1.UploadRestore(LocalFile, RemoteFile, FPosition);
          end;
  finally
    O.Free;
  end;
end;

When Button8 is clicked, an OpenDialog is displayed to select a file on the local computer to upload to the remote host. If the Open button is clicked, the InputQuery function is used to retrieve the name the file will use on the remote host. The InputQuery function is called once more to obtain the position to restore the upload from. If the Ok button is clicked, the Mode method is used to set the mode as determined by RadioGroup1. The UploadRestore method continues a previously interrupted upload at the position specified.

Insert the following code into Button9\'s OnClick event:

procedure TForm1.Button9Click(Sender: TObject);
begin
  NMFTP1.Abort;
end;

When  Button9 is clicked, the Abort method is called, aborting the current operation.

Snowball
Avatar billede gozar Nybegynder
20. august 2000 - 19:44 #5
He he.. Intet skal være nemt..

Okay, forsøger det, og vender tilbage!!!
Avatar billede snowball Novice
20. august 2000 - 19:54 #6
Jeg har tidligere afprøvet eksemplet og det virker faktisk ganske udemærket !

Snowball
Avatar billede gozar Nybegynder
20. august 2000 - 21:00 #7
Snowball Jeg får stadig fejl i case comandoen!!
Jeg bruger også lisview istedet for memo. Det skal bruges til listning af fileren på frp serveren. Ikke bare skrive, hvad der sker!! Se oxo såm http://www.eksperten.dk/spm/20219
Hvis det kan hjælpe.

Jeg har forsøgt at bruge noget af min genialitet (Fnis) ok er kommet frem til at dette her måske er tæt på. Men der sket ikke noget i min Listviev komponent!!
  -- Kode start --

procedure TForm1.Button5Click(Sender: TObject);
begin
Server.Items.Clear;
FTP1.List;
end;

procedure TForm1.FTP1ListItem(Listing: String);
Var
  Fsr : TSearchRec;
  SogSti : String;
  ListItem: TListItem;
  Sti : String;
Begin
  Sti := (Listing);
  If Sti[Length(Sti)] = \'\\\' then else
    Sti := Sti + \'\\\';
  If LastDelimiter(\'g\',Sti) > 0 then else
    SogSti := Sti + \'*.*\';

  If FindFirst(SogSti, faAnyFile, Fsr) = 0 then
    If FSr.Attr <> faDirectory then
      Begin
        ListItem := Server.Items.Add;
        ListItem.Caption := Fsr.Name;
        ListItem.SubItems.Add(ExtractFileExt(Fsr.Name)); //Denne linie tilføjer efternavnet i den næste celle.
        ListItem.SubItems.Add(IntToStr(Fsr.Size));
      end;

  While FindNext(Fsr) = 0 do
    If FSr.Attr <> faDirectory then
      Begin
        ListItem := Server.Items.Add;
        ListItem.Caption := Fsr.Name;
        ListItem.SubItems.Add(ExtractFileExt(Fsr.Name)); //Denne linie tilføjer efternavnet i den næste celle.
        ListItem.SubItems.Add(IntToStr(Fsr.Size));
      end;
  FindClose(Fsr);
end;

end.

  -- Kode Slut --
Avatar billede snowball Novice
20. august 2000 - 21:11 #8
øøøhhhh......case sætning si\'r du lige !? Der er ikke meget case sætning i det kode du lige har skrevet !

Kan du ikke lige prøve og maile det hele til michael@geertsen.net ???

Snowball
Avatar billede gozar Nybegynder
21. august 2000 - 01:05 #9
Nej, men det er der i den kode du har skrevet!!!
Avatar billede snowball Novice
21. august 2000 - 06:03 #10
Kigger lige på det når jeg kommer hjem igen senere i dag !

Snowball
Avatar billede gozar Nybegynder
21. august 2000 - 08:39 #11
Helt iorden. Kan heller ikkel lave noget før jeg selv kommer hjem!
Avatar billede gozar Nybegynder
31. august 2000 - 12:59 #12
Tager point igen, da det ikek ser ud til at jeg får besvaret mit spm.

Elders tak for hjælpen Snowball
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester