#[solved] How to check if a folder exists at runtime
1 messages ยท Page 1 of 1 (latest)
can't you just try and open the folder?
i'd expect such a function to return error.FileNotFound or the like, when missing the folder
access is pretty much always a race, use open if possible
rather than checking if resource exists to then do something with it (might get created/removed between the check and the actual logic)... open the file and operate while open (or something like that)
I just want to check if it exists, and do something with it immediately
That sounds way too complicated for the usecase? ๐ค
until a user just happens to delete the file in between the 2 things and you crash anyway, even though you checked that it exists ๐
If the user is able to delete the file in a fraction of a milisecond in between two lines of my code... they have bigger problems than my app crashing ๐ค
guess i should've asked if you are doing anything with the file other than checking its existence
perhaps this is a lock file and all you do with it is existing/missing
Nothing, that's the point. I just check if the folder exists and create it if it doesn't
but even then, i'd still open the file as a way to ensure that i dont run into edge cases
essentially mkdir -p somedir/
But I don't know how to do that in Zig for folders, only for files
Is there a way to do that?
this seems like it would do? just the 1st thing i found looking for "open" on the fs' doc page
there might be some similar API (or a shortcut to this one) if you have a fs.Dir + relative path, idk
Well that's the starting question! ๐
no, your question was to check for folder's existence
not "create file if not exists" ๐
???
I want to check if a folder exists, and create it if it doesn't exist?
That is NOT files, its folder, like the question stated
yeah, but i mean we diverged to "should not check, then operate, can be a race" which then became "why are you checking if file exists?"
Yes, and I reverted back to the actual topic by saying "that was the original question"
Man this argument is ridiculous
Please lets focus on how to check if a folder exists at runtime ๐
creates if not there yet, else returns PathAlreadyExists (you wanted mkdir -p, this sounds like it)
as the docstring says, may want to use makePath if you could be missing other dirs further up the path
Thanks! โ๏ธ
and don't wanna sound rude or anything, but all i did was 5 seconds of Ctrl+F on the fs' docspage
My LSP is super broken, so I cannot easily find new/unknown functions that I haven't used yet ๐
i haven't really used fs APIs much
Should start doing that, yea. My brain keeps forgetting I could search instead of LSP
I don't know how that makeDir will work if I just want to check existence without creation though
you wrote this, though :v
Yes, true. Because it is applicable to this one usecase
But its a follow up question
^
Why would makeDir return an error on not existing? Like the whole point of it is create it if it doesn't exist?
was not talking bout makeDir
๐คฆโโ๏ธ
You are confusing the hell out of me.
Lets mark the question solved and I will open another if I ever run into the other case
[solved] How to check if a folder exists at runtime
thanks for the help
fn folderExists(path: []const u8) bool {
var dir = std.fs.openDirAbsolute(path) catch |e| switch (e) {
error.FileNotFound => return false,
else => // TODO: whatever you want for other errors
};
dir.close();
return true;
}
i mean if you write this, you might as well access. usually you want to do something with the dir after though
up to OP, seems like checking and then not doing anything with it is a potential thing their code might do