Files
file-organizer/src/organize.zig
T

27 lines
842 B
Zig

const std = @import("std");
const classify = @import("classify.zig");
pub fn organize(io: std.Io, dir: std.Io.Dir, name: []const u8) !void {
const category = classify.classify(name);
try placeFile(io, dir, category, name);
}
fn placeFile(io: std.Io, dir: std.Io.Dir, category: classify.Category, name: []const u8) !void {
const dest = switch (category) {
.image => "Pictures",
.document => "Documents",
.music => "Music",
.video => "Videos",
.archive => "Archives",
.code => "Code",
.other => return,
};
try dir.createDirPath(io, dest);
var buf: [std.fs.max_path_bytes]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
const path = try std.fs.path.join(fba.allocator(), &.{ dest, name });
try dir.rename(name, dir, path, io);
}