Sample code for 30+ languages & platforms
PowerBuilder

Delete Multiple Remote FTP Files by Wildcard

See more FTP Examples

Demonstrates the Chilkat Ftp2.DeleteMatching method, which deletes files in the current remote directory whose names match a wildcard pattern. The only argument is the pattern. It returns the number deleted, or -1 on failure.

Background: This is a convenience for bulk cleanup — deleting all *.tmp or *.log files in one call rather than listing and looping yourself. It operates only on the current directory and does not recurse. The count return lets you confirm how many were removed, distinct from the -1 failure signal; because deletion is permanent, double-check the pattern before running it.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ftp
integer li_NumDeleted

li_Success = 0

//  Demonstrates the Ftp2.DeleteMatching method, which deletes files in the current remote directory
//  whose names match a wildcard pattern.  The only argument is the pattern.  It returns the number
//  deleted, or -1 on failure.

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

//  Delete all .tmp files in the current remote directory.
li_NumDeleted = loo_Ftp.DeleteMatching("*.tmp")
if li_NumDeleted < 0 then
    Write-Debug loo_Ftp.LastErrorText
    destroy loo_Ftp
    return
end if

Write-Debug "Deleted " + string(li_NumDeleted) + " files."

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



destroy loo_Ftp