Sample code for 30+ languages & platforms
Zig

Download Multiple Files Matching Pattern

See more FTP Examples

The MGetFiles method can be called to download all files matching a wildcarded filename pattern.

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("ftp.example.com");
    ftp.setUsername("myUsername");
    ftp.setPassword("myPassword");
    ftp.setPort(21);
    ftp.setAuthTls(true);

    // 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 are located.
    // This step is only necessary if the files are not in the root directory
    // of the FTP account.
    ftp.changeRemoteDir("qa") catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Download all files with filenames matching "*.txt";
    // The files are downloaded into c:/temp/qa_output
    const num_files_downloaded = ftp.mGetFiles("*.txt", "c:/temp/qa_output");
    if (num_files_downloaded < 0) {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    }

    try ftp.disconnect();

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