Zig
Zig
SFTP Download Files Matching a Pattern
See more SFTP Examples
Demonstrates how to download files in a directory matching one or more patterns (such as "*.zip" or "abc*_*0719.csv".Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
var success: bool = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const sftp = try chilkat.SFtp.init();
defer sftp.deinit();
success = if (sftp.connect("my-ssh-server.com", 22)) true else |_| false;
if (success) {
success = if (sftp.authenticatePw("mySshLogin", "mySshPassword")) true else |_| false;
}
if (success) {
success = if (sftp.initializeSftp()) true else |_| false;
}
if (!success) {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
}
// The SyncTreeDownload method can be used non-recursively to download all files matching a set of patterns.
// This example will download all files, but only those files having filenames
// that match *.csv and *.eml
sftp.setSyncMustMatch("*.eml; *.gif");
const remote_dir = "syncDownloadTest/someDir";
const local_dir = "qa_output";
// mode=0: Download all matching files according to SyncMustMatch
const mode = 0;
// do not recursively traverse the remote directory tree.
const recursive = false;
sftp.syncTreeDownload(remote_dir, local_dir, mode, recursive) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
std.debug.print("Success.\n", .{});
}