Unicode C
Unicode C
SFTP Read Directory Listing
See more SFTP Examples
Demonstrates how to download a directory listing and iterate over the files.Chilkat Unicode C Downloads
#include <C_CkSFtpW.h>
#include <C_CkSFtpDirW.h>
#include <C_CkSFtpFileW.h>
void ChilkatSample(void)
{
BOOL success;
HCkSFtpW sftp;
const wchar_t *hostname;
int port;
const wchar_t *handle;
HCkSFtpDirW dirListing;
HCkSFtpFileW fileObj;
int i;
int n;
success = FALSE;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Important: It is helpful to send the contents of the
// sftp.LastErrorText property when requesting support.
sftp = CkSFtpW_Create();
// Set some timeouts, in milliseconds:
CkSFtpW_putConnectTimeoutMs(sftp,5000);
CkSFtpW_putIdleTimeoutMs(sftp,10000);
// Connect to the SSH server.
// The standard SSH port = 22
// The hostname may be a hostname or IP address.
hostname = L"www.my-sftp-server.com";
port = 22;
success = CkSFtpW_Connect(sftp,hostname,port);
if (success == FALSE) {
wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
CkSFtpW_Dispose(sftp);
return;
}
// Authenticate with the SSH server. Chilkat SFTP supports
// both password-based authenication as well as public-key
// authentication. This example uses password authenication.
success = CkSFtpW_AuthenticatePw(sftp,L"myLogin",L"myPassword");
if (success == FALSE) {
wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
CkSFtpW_Dispose(sftp);
return;
}
// After authenticating, the SFTP subsystem must be initialized:
success = CkSFtpW_InitializeSftp(sftp);
if (success == FALSE) {
wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
CkSFtpW_Dispose(sftp);
return;
}
// Open a directory on the server...
// Paths starting with a slash are "absolute", and are relative
// to the root of the file system. Names starting with any other
// character are relative to the user's default directory (home directory).
// A path component of ".." refers to the parent directory,
// and "." refers to the current directory.
handle = CkSFtpW_openDir(sftp,L".");
if (CkSFtpW_getLastMethodSuccess(sftp) == FALSE) {
wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
CkSFtpW_Dispose(sftp);
return;
}
// Download the directory listing:
dirListing = CkSFtpDirW_Create();
success = CkSFtpW_ReadDirListing(sftp,handle,dirListing);
if (success == FALSE) {
wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
CkSFtpW_Dispose(sftp);
CkSFtpDirW_Dispose(dirListing);
return;
}
// Close the handle for the directory listing.
success = CkSFtpW_CloseHandle(sftp,handle);
if (success == FALSE) {
wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
CkSFtpW_Dispose(sftp);
CkSFtpDirW_Dispose(dirListing);
return;
}
// Iterate over the files.
fileObj = CkSFtpFileW_Create();
i = 0;
n = CkSFtpDirW_getNumFilesAndDirs(dirListing);
while (i < n) {
success = CkSFtpDirW_FileAt(dirListing,i,fileObj);
if (success == FALSE) {
wprintf(L"%s\n",CkSFtpDirW_lastErrorText(dirListing));
CkSFtpW_Dispose(sftp);
CkSFtpDirW_Dispose(dirListing);
CkSFtpFileW_Dispose(fileObj);
return;
}
wprintf(L"%s\n",CkSFtpFileW_filename(fileObj));
wprintf(L"%s\n",CkSFtpFileW_fileType(fileObj));
wprintf(L"Size in bytes: %d\n",CkSFtpFileW_getSize32(fileObj));
wprintf(L"----\n");
i = i + 1;
}
wprintf(L"Success.\n");
CkSFtpW_Dispose(sftp);
CkSFtpDirW_Dispose(dirListing);
CkSFtpFileW_Dispose(fileObj);
}