Is there a way to change the current working directory?

I’m developing a library that manages files and I want the client to ba able to select where the working directory is so it can change the relative paths for opening files. The C++ equivalent is std::filesystem::current_path(working_dir_str); but I can’t find any Zig equivalent.

You can use std.os.chdir to change the current directory.

3 Likes

There’s also std.fs.Dir.setAsCwd if you have a Dir:

var dir = try std.fs.cwd().openDir("some-path/to/dir", .{});
defer dir.close();

try dir.setAsCwd();
4 Likes