Is there a way in basic-cli to check if a directory exists? I see there is FileMetadata
which has type of the file, but I'm not sure how to get it
Besides that I think I could do
Cmd.new "test"
|> Cmd.args ["-d", pathToDir]
|> Cmd.status
Are there any other options currently?
I don't think so...we could add File.exists : Path -> Task Bool [FileReadErr Path ReadErr]
as a convenience - does that sound good?
Perhaps might as well make it Path.exists, so it works for dirs as well. Rust has Path.exists() too.
I figured directories are a type of file...personally I've always been bothered by that being in path on Rust :big_smile:
I think of Path
as being where I go to for transforming Path
data structures, not doing actual I/O
although I guess it does do I/O for things like canonicalize
Richard Feldman said:
I don't think so...we could add
File.exists : Path -> Task Bool [FileReadErr Path ReadErr]
as a convenience - does that sound good?
This would also be a good thing to add, but I was thing more of something like Path::is_dir
in rust, to specifically check for directories
Right now I'm doing something like this:
checkDirExists : Path -> Task Bool _
checkDirExists = \path ->
result <- Cmd.new "test"
|> Cmd.args ["-d", Path.display path]
|> Cmd.status
|> Task.attempt
when result is
Ok _ -> Task.ok Bool.true
Err (ExitCode 1) -> Task.ok Bool.false
Err err -> Task.err err
which is... a little bit wordy
ahh gotcha
how do you feel about File.isDir
as the name?
I would prefer Path here as well.
I understand what you mean by "I figured directories are a type of file..." but.. I believe the majority of us in the Windows ecosystem will look at something like "File.isDir" as a paradox :sweat_smile: "What do you mean a file is a directory? How is that not hardcoded to false
?"
We pretty much see files as one thing as directories as another, regardless of how they are stored under the hood. I agree that Path.isDir
feels more adequate. You could even have Path.getInfo which would return a union NonExistent | Directory DirInfo | File FileInfo
but that could be a whole different can of worms :sweat_smile:
Last updated: Jul 06 2025 at 12:14 UTC