Sample code for 30+ languages & platforms
Zig

Delete Files Matching Pattern

See more FTP Examples

The DeleteMatching method deletes all files in the current remote directory matching a wildcarded filename.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const ftp = try chilkat.Ftp2.init();
    defer ftp.deinit();

    ftp.setHostname("www.chilkatsoft.com");
    ftp.setUsername("MyLogin");
    ftp.setPassword("MyPassword");

    // Connect and login to the FTP server.
    ftp.connect() catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Change to the remote directory where the files to be deleted are located.
    // for the FTP account.
    ftp.changeRemoteDir("junk") catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Delete all files with filenames matching "ftp_*.asp"
    const num_deleted = ftp.deleteMatching("ftp_*.asp");
    if (num_deleted < 0) {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    }

    try ftp.disconnect();

    std.debug.print("{d} Files Deleted!\n", .{num_deleted});
}