Unicode C
Unicode C
Delete Local Files that Do Not Exist on the FTP Server
See more FTP Examples
Demonstrates how to get a list of local files in a directory tree that do not exist on the FTP server.Chilkat Unicode C Downloads
#include <C_CkFtp2W.h>
#include <C_CkStringArrayW.h>
#include <C_CkFileAccessW.h>
void ChilkatSample(void)
{
BOOL success;
HCkFtp2W ftp;
int mode;
BOOL descendTree;
BOOL previewOnly;
HCkStringArrayW sa;
HCkFileAccessW fac;
int numFiles;
int i;
const wchar_t *localFilePath;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
ftp = CkFtp2W_Create();
CkFtp2W_putHostname(ftp,L"ftp.example.com");
CkFtp2W_putUsername(ftp,L"login");
CkFtp2W_putPassword(ftp,L"password");
CkFtp2W_putKeepSessionLog(ftp,TRUE);
// Connect and login to the FTP server.
success = CkFtp2W_Connect(ftp);
if (success != TRUE) {
wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
CkFtp2W_Dispose(ftp);
return;
}
// Set the current remote directory to the root of
// the remote tree to be compared.
success = CkFtp2W_ChangeRemoteDir(ftp,L"abc123");
if (success != TRUE) {
wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
CkFtp2W_Dispose(ftp);
return;
}
// Recursively descend the local directory tree
// and find the files that exist locally but not remotely.
// These are the files what would be uploaded via
// the SyncRemoteTree method call with mode = 1.
// (Mode 1 would upload all files that do not exist on the FTP server.)
// The actual uploading is avoided by setting the preview-only argument to TRUE.
mode = 1;
descendTree = TRUE;
previewOnly = TRUE;
success = CkFtp2W_SyncRemoteTree2(ftp,L"/temp/abc123",mode,descendTree,previewOnly);
if (success != TRUE) {
wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
CkFtp2W_Dispose(ftp);
return;
}
// The files what would've been uploaded are now available in the SyncPreview property,
// which contains a list of local file paths, one per line.
// A program can iterate over them like this:
sa = CkStringArrayW_Create();
CkStringArrayW_LoadFromText(sa,CkFtp2W_syncPreview(ftp));
fac = CkFileAccessW_Create();
numFiles = CkStringArrayW_getCount(sa);
i = 0;
while ((i < numFiles)) {
localFilePath = CkStringArrayW_getString(sa,i);
wprintf(L"%s\n",localFilePath);
// An application can delete the file using Chilkat's file access object,
// or it can choose to use the native file API available in the programming language:
success = CkFileAccessW_FileDelete(fac,localFilePath);
if (success != TRUE) {
wprintf(L"Failed to delete: %s\n",localFilePath);
}
i = i + 1;
}
success = CkFtp2W_Disconnect(ftp);
CkFtp2W_Dispose(ftp);
CkStringArrayW_Dispose(sa);
CkFileAccessW_Dispose(fac);
}