Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Rename or Move a Remote File or Directory

See more SFTP Examples

Demonstrates the Chilkat SFtp.RenameFileOrDir method, which renames or moves a remote file or directory. The first argument is the old path and the second is the new path.

Background: Rename and move are the same operation in SFTP: changing only the final path component renames in place, while changing the directory portion relocates the item. When source and destination are on the same filesystem the server does this as a fast metadata change rather than copying data, which is why moving even a large file is near-instant. Moving across filesystems, or onto an existing target, may fail depending on the server.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.SFtp;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  sftp: TSFtp;
  port: Integer;
  password: string;

begin
  success := False;

  //  Demonstrates the SFtp.RenameFileOrDir method, which renames or moves a remote file or
  //  directory.  The 1st argument is the old path and the 2nd is the new path.

  sftp := TSFtp.Create;

  //  Connect, authenticate, and initialize the SFTP subsystem.
  port := 22;
  success := sftp.Connect('sftp.example.com',port);
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  //  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.
  password := 'mySshPassword';

  success := sftp.AuthenticatePw('mySshLogin',password);
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  success := sftp.InitializeSftp();
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  //  Rename a file within the same directory.
  success := sftp.RenameFileOrDir('subdir/report.txt','subdir/report_final.txt');
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  //  Because rename can also move, the same method relocates an item to a different directory.
  success := sftp.RenameFileOrDir('subdir/report_final.txt','archive/report_final.txt');
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;
  WriteLn('Renamed and moved.');

  sftp.Disconnect();


  sftp.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.