Sample code for 30+ languages & platforms
Zig

Check if an FTP Listing Entry is a Directory

See more FTP Examples

Demonstrates the Chilkat Ftp2.GetIsDirectory method, which returns whether a cached directory-listing entry is a directory. The only argument is a zero-based index. An invalid index returns false.

Background: Distinguishing directories from files is the first thing most listing code needs — to render a folder tree, to decide whether to recurse, or to apply different handling. This reads the type from the cached listing, so it is a local lookup rather than a server query. Reliability depends on the server's listing format conveying the type, which the common Unix and MLSD formats do.

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();

    // Demonstrates the Ftp2.GetIsDirectory method, which returns whether a cached directory-listing
    // entry is a directory.  The only argument is a zero-based index into the current listing.

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

    ftp.setHostname("ftp.example.com");
    ftp.setUsername("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.
    ftp.setPassword("myPassword");

    ftp.connect() catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    ftp.changeRemoteDir("public_html") catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // GetDirCount retrieves the directory listing on the first call and returns the number of
    // entries.  A negative return indicates failure.
    const n = ftp.getDirCount();
    if (n < 0) {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    }

    var i: i32 = 0;
    i = 0;
    while (i < n) : (i += 1) {
        const name = try ftp.getFilename(alloc, i);
        if (ftp.getIsDirectory(i)) {
            std.debug.print("[DIR]  {s}\n", .{name});
        } else {
            std.debug.print("       {s}\n", .{name});
        }
    }

    ftp.disconnect() catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };
}