Stream: beginners

Topic: basic cli: check if directory exists


view this post on Zulip Chris (Aug 23 2023 at 18:11):

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?

view this post on Zulip Richard Feldman (Aug 23 2023 at 18:17):

I don't think so...we could add File.exists : Path -> Task Bool [FileReadErr Path ReadErr] as a convenience - does that sound good?

view this post on Zulip Anton (Aug 23 2023 at 18:18):

Perhaps might as well make it Path.exists, so it works for dirs as well. Rust has Path.exists() too.

view this post on Zulip Richard Feldman (Aug 23 2023 at 18:19):

I figured directories are a type of file...personally I've always been bothered by that being in path on Rust :big_smile:

view this post on Zulip Richard Feldman (Aug 23 2023 at 18:20):

I think of Path as being where I go to for transforming Path data structures, not doing actual I/O

view this post on Zulip Richard Feldman (Aug 23 2023 at 18:20):

although I guess it does do I/O for things like canonicalize

view this post on Zulip Chris (Aug 23 2023 at 18:28):

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

view this post on Zulip Chris (Aug 23 2023 at 18:37):

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

view this post on Zulip Richard Feldman (Aug 23 2023 at 18:42):

ahh gotcha

view this post on Zulip Richard Feldman (Aug 23 2023 at 18:42):

how do you feel about File.isDir as the name?

view this post on Zulip Anton (Aug 24 2023 at 08:59):

I would prefer Path here as well.

view this post on Zulip Fábio Beirão (Aug 24 2023 at 14:48):

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