The program represents fully functional command-line FTP client.

Download Source Code

var
  ftpClient: TclConsoleFtpClient;
begin
  try
    ftpClient := TclConsoleFtpClient.Create();
    try
      ftpClient.Run();
    finally
      ftpClient.Free();
    end;
  except
    on E:Exception do
      WriteLn(E.Classname, ': ', E.Message);
  end;
end.

procedure TclConsoleFtpClient.Run;
var
  s: string;
  cmd: TStrings;
  isquit: Boolean;
begin
  cmd := TStringList.Create();
  try
    isquit := False;
    repeat
      try
        ReadLn(s);
        ExtractQuotedWords(s, cmd);
        isquit := HandleCommands(cmd);
      except
        on E: EclSocketError do
        begin
          HandleSocketError(E);
        end;
        on E: EclConsoleFtpError do
        begin
          HandleConsoleFtpError(E);
        end;
      end;
    until isquit;
  finally
    cmd.Free();
  end;
end;

function TclConsoleFtpClient.HandleCommands(ACmd: TStrings): Boolean;
var
  name: string;
begin
  Result := False;
   
  if (ACmd.Count = 0) then
  begin
    HandleUnknownCommand();
  end;
 
  name := LowerCase(ACmd[0]);
 
  if (name = 'help') then
  begin
    HandleHelp();
  end else
  if (name = 'open') then
  begin
    HandleOpen(ACmd);
  end else
  if (name = 'close') then
  begin
    HandleClose();
  end else
 
....
 
  end else
  begin
    HandleUnknownCommand();
  end;
end;

procedure TclConsoleFtpClient.HandleOpen(ACmd: TStrings);
var
  s: string;
begin
  if ACmd.Count < 2 then
  begin
    HandleWrongParameters(ACmd[0]);
  end;
 
  FClient.Server := ACmd[1];
 
  if ACmd.Count > 2 then
  begin
    FClient.Port := StrToIntDef(ACmd[2], cDefaultFtpPort);
  end;
 
  Write('User: ');
  ReadLn(s);
  FClient.UserName := s;
 
  Write('Password: ');
  FClient.Password := GetPassword();
  WriteLn('');
   
  FClient.Open();
 
  WriteLn('Connected.');
   
  HandlePwd();
end;

Article ID: 73, Created On: 12/19/2013, Modified: 12/20/2013