Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Remove a Remote FTP Directory (RMD)
See more FTP Examples
Demonstrates the Chilkat Ftp2.RemoveRemoteDir method, which removes a remote directory. The only argument is the remote directory path.
Background: Most FTP servers refuse to remove a directory that is not empty, so clearing a populated folder means deleting its contents first — or using
DeleteTree to clear the subtree and then removing the now-empty directory. After a removal, the cached indexed listing is not refreshed automatically; call ClearDirCache before relying on the old cache again.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Ftp2;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
ftp: TFtp2;
begin
success := False;
// Demonstrates the Ftp2.RemoveRemoteDir method, which removes a remote directory. The only
// argument is the remote directory path.
ftp := TFtp2.Create;
ftp.Hostname := 'ftp.example.com';
ftp.Username := 'myFtpLogin';
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
ftp.Password := 'myPassword';
success := ftp.Connect();
if (success = False) then
begin
WriteLn(ftp.LastErrorText);
Exit;
end;
success := ftp.ChangeRemoteDir('public_html');
if (success = False) then
begin
WriteLn(ftp.LastErrorText);
Exit;
end;
// FTP servers generally require the directory to be empty before it can be removed.
success := ftp.RemoveRemoteDir('uploads/old');
if (success = False) then
begin
WriteLn(ftp.LastErrorText);
Exit;
end;
WriteLn('Directory removed.');
success := ftp.Disconnect();
if (success = False) then
begin
WriteLn(ftp.LastErrorText);
Exit;
end;
ftp.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.