#[solved] How to check if a folder exists at runtime

1 messages ยท Page 1 of 1 (latest)

graceful vale
#
return if (someDir.access("somefile.ext", .{.mode= .read_only})) |_| true else |_| false;
```I have this for files, but is there something for folders instead?
visual pine
#

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

steep summit
#

access is pretty much always a race, use open if possible

graceful vale
#

I don't know how to do that

#

What function/s should I be using?

visual pine
# graceful vale I don't know how to do that

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)

graceful vale
#

I just want to check if it exists, and do something with it immediately

#

That sounds way too complicated for the usecase? ๐Ÿค”

visual pine
#

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 ๐Ÿ˜›

graceful vale
#

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 ๐Ÿค”

visual pine
#

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

graceful vale
#

Nothing, that's the point. I just check if the folder exists and create it if it doesn't

visual pine
#

but even then, i'd still open the file as a way to ensure that i dont run into edge cases

graceful vale
#

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?

visual pine
#

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

graceful vale
#

Well that's the starting question! ๐Ÿ™ˆ

visual pine
#

no, your question was to check for folder's existence

#

not "create file if not exists" ๐Ÿ˜›

graceful vale
#

???

#

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

visual pine
#

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?"

graceful vale
#

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 ๐Ÿ™

visual pine
#

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

graceful vale
#

Thanks! โœ๏ธ

visual pine
#

and don't wanna sound rude or anything, but all i did was 5 seconds of Ctrl+F on the fs' docspage

graceful vale
#

My LSP is super broken, so I cannot easily find new/unknown functions that I haven't used yet ๐Ÿ™

visual pine
#

i haven't really used fs APIs much

graceful vale
#

I don't know how that makeDir will work if I just want to check existence without creation though

visual pine
graceful vale
#

Yes, true. Because it is applicable to this one usecase
But its a follow up question

graceful vale
#

Why would makeDir return an error on not existing? Like the whole point of it is create it if it doesn't exist?

visual pine
#

๐Ÿคฆโ€โ™‚๏ธ

graceful vale
#

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

visual pine
#
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;
}
steep summit
visual pine
#

up to OP, seems like checking and then not doing anything with it is a potential thing their code might do