Sample code for 30+ languages & platforms
React Native

SFTP Download all Files in a Directory

See more SFTP Examples

Demonstrates how to download all files in a remote directory. This example uses the SyncTreeDownload method in a non-recursive mode that forces the download of all files, regardless of date/time or size differences.

Chilkat React Native Downloads

React Native
import { SFtp } from '@chilkat/react-native'

async function chilkatExample() {
  let success = false;

  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const sftp = new SFtp();

  success = true;
  try {
    await sftp.connectAsync('my-ssh-server.com', 22);
  } catch {
    success = false;
  }
  if (success) {
    success = true;
    try {
      await sftp.authenticatePwAsync('mySshLogin', 'mySshPassword');
    } catch {
      success = false;
    }
  }

  if (success) {
    success = true;
    try {
      await sftp.initializeSftpAsync();
    } catch {
      success = false;
    }
  }

  if (!success) {
    console.log(sftp.lastErrorText);
    return;
  }

  // Download all the files from the remote directory "syncDownloadTest/xml"
  // into our local directory "qa_output"
  // Both directories are relative paths.  The remote directory
  // is relative to the HOME directory of the SSH user account.
  // The local directory is relative to the current working directory of the process.
  // It is also possible to use absolute paths.

  const remoteDir = 'syncDownloadTest/xml';
  const localDir = 'qa_output';
  // Mode 0 causes SyncTreeDownload to download all files.
  const mode = 0;
  // Do not recursively descend the remote directory tree.  Just download all the files in specified directory.
  const recursive = false;
  success = true;
  try {
    await sftp.syncTreeDownloadAsync(remoteDir, localDir, mode, recursive);
  } catch {
    success = false;
  }
  if (!success) {
    console.log(sftp.lastErrorText);
    return;
  }

  console.log('Success.');
}