Sample code for 30+ languages & platforms
Zig

Get FTP Directory Listing Information

See more FTP Examples

_LANGUAGE_ example showing how to get information about files and subdirectories in the current remote FTP directory.

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;
    };

    // The ListPattern property is our directory listing filter.
    // The default value is "*", which includes everything.
    std.debug.print("{s}\n", .{try ftp.getListPattern(alloc)});

    // To get file and sub-directory information, simply
    // loop from 0 to ftp.GetDirCount() - 1
    var i: i32 = 0;
    var n: i32 = 0;
    n = ftp.getDirCount();
    if (n < 0) {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    }

    if (n > 0) {
        i = 0;
        while (i < n) : (i += 1) {
            // Display the filename
            std.debug.print("{s}\n", .{try ftp.getFilename(alloc, i)});

            // Display the file size (in bytes)
            std.debug.print("{d}\n", .{ftp.getSize(i)});

            // Is this a sub-directory?
            if (ftp.getIsDirectory(i)) {
                std.debug.print(".. this is a sub-directory\n", .{});
            }

            std.debug.print("--\n", .{});
        }
    }

    std.debug.print("-----------------------------------\n", .{});

    // Only files and directories
    // matching the ListPattern are returned.
    ftp.setListPattern("*.asp");
    n = ftp.getDirCount();
    if (n < 0) {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    }

    if (n > 0) {
        i = 0;
        while (i < n) : (i += 1) {
            // Display the filename
            std.debug.print("{s}\n", .{try ftp.getFilename(alloc, i)});

            // Display the file size (in bytes)
            std.debug.print("{d}\n", .{ftp.getSize(i)});

            std.debug.print("--\n", .{});
        }
    }

    try ftp.disconnect();
}