Sample code for 30+ languages & platforms
PowerBuilder

Delete Remote FTP Files Missing Locally (Sync)

See more FTP Examples

Demonstrates the Chilkat Ftp2.SyncDeleteRemote method, which recursively compares the remote tree with a local tree and deletes remote files that have no corresponding local file. The only argument is the local root.

Note: The local paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This provides the "delete extras" half of a true mirror: SyncRemoteTree adds and updates, and SyncDeleteRemote removes remote files that no longer exist in the source, so together they make the server match the local directory exactly. Because it deletes, run it deliberately — a preview upload with SyncRemoteTree2 first, and confidence that the local tree is the intended source of truth, guard against removing files you meant to keep.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ftp

li_Success = 0

//  Demonstrates the Ftp2.SyncDeleteRemote method, which recursively compares the remote tree with a
//  local tree and deletes remote files that have no corresponding local file.  The only argument
//  is the local root.

loo_Ftp = create oleobject
li_rc = loo_Ftp.ConnectToNewObject("Chilkat.Ftp2")
if li_rc < 0 then
    destroy loo_Ftp
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Ftp.Hostname = "ftp.example.com"
loo_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.
loo_Ftp.Password = "myPassword"

li_Success = loo_Ftp.Connect()
if li_Success = 0 then
    Write-Debug loo_Ftp.LastErrorText
    destroy loo_Ftp
    return
end if

li_Success = loo_Ftp.ChangeRemoteDir("public_html")
if li_Success = 0 then
    Write-Debug loo_Ftp.LastErrorText
    destroy loo_Ftp
    return
end if

//  This makes the remote tree match the local tree by removing remote-only files.  Combine it
//  with SyncRemoteTree (upload) to make the remote an exact mirror of the local directory.
li_Success = loo_Ftp.SyncDeleteRemote("qa_data/site")
if li_Success = 0 then
    Write-Debug loo_Ftp.LastErrorText
    destroy loo_Ftp
    return
end if

Write-Debug "Removed remote files not present locally."

li_Success = loo_Ftp.Disconnect()
if li_Success = 0 then
    Write-Debug loo_Ftp.LastErrorText
    destroy loo_Ftp
    return
end if



destroy loo_Ftp