Sample code for 30+ languages & platforms
Zig

Quote and SendCommand

See more FTP Examples

Demonstrate the Quote and SendCommand methods.

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("login");
    ftp.setPassword("password");

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

    // Tell the FTP object to keep an in-memory session log
    // so we can see the commands sent to the server,
    // and the responses received back.
    ftp.setKeepSessionLog(true);

    // Change the current remote directory via the Quote method:
    ftp.quote("CWD junk") catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Move back up
    // In this case, ChangeRemoteDir sends "CWD .." to the FTP server.
    ftp.changeRemoteDir("..") catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Do the same via the SendCommand method where the
    // raw FTP server response is returned:
    const server_response = ftp.sendCommand(alloc, "CWD junk") catch "";
    if (!ftp.getLastMethodSuccess()) {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
    } else {
        std.debug.print("{s}\n", .{server_response});
    }

    try ftp.disconnect();

    std.debug.print("Session Log:\n", .{});
    std.debug.print("{s}\n", .{try ftp.getSessionLog(alloc)});
}