File

Config & IO

Filesystem helpers for the BEAM target.

This is filesystem I/O on the server node, not a browser file picker. Use these tasks to read and write paths.

For large files, prefer stream / streamBytes with Stream.foldl or Stream.attemptFoldl so lines or chunks are processed one at a time instead of loading the whole file with read.

This module is selm stdlib for target = "erlang". Browser apps use the client platform File (name / toString / File.Select), not read / exists / write. See fixtures/file_dual and examples/14_ws_books_upload.

Types

Error

type Error
= ReadFailed String
| WriteFailed String
| IoFailed String

Why a filesystem task failed.

Examples

Err (File.ReadFailed "enoent")

Stat

type alias Stat =
{ size : Int
, mtimeMs : Int
, isDir : Bool
, isRegular : Bool
}

Metadata from stat.

Examples

File.stat "README.md"

Values

read

read : String -> Task Error Bytes

Read an entire file into Bytes.

Examples

File.read "README.md"

write

write : String -> Bytes -> Task Error ()

Write Bytes to a path, creating or replacing the file.

Examples

File.write "out.txt" bytes

exists

exists : String -> Task Error Bool

True when the path exists.

Examples

File.exists "README.md"

stat

stat : String -> Task Error Stat

File or directory metadata.

Examples

File.stat "README.md"

mkdir

mkdir : String -> Task Error ()

Create a single directory. Parent must already exist.

Examples

File.mkdir "tmp"

mkdirAll

mkdirAll : String -> Task Error ()

Create a directory and any missing parents.

Examples

File.mkdirAll "tmp/a/b"

delete

delete : String -> Task Error ()

Delete a file or empty directory.

Examples

File.delete "tmp/out.txt"

rename

rename : String -> String -> Task Error ()

Rename or move a path.

Examples

File.rename "a.txt" "b.txt"

copy

copy : String -> String -> Task Error ()

Copy a file to a new path.

Examples

File.copy "a.txt" "b.txt"

list

list : String -> Task Error (List String)

List immediate names under path (files and directories, sorted, no . / ..).

Examples

File.list "stdlib"

listDirs

listDirs : String -> Task Error (List String)

List immediate subdirectory names under path (sorted, no . / ..).

Examples

File.listDirs "stdlib"

stream

stream : String -> Stream String

Lazily stream file lines (newline stripped). Opens on first pull; closes when the stream is exhausted or stopped early (Stream.take, discarded tail).

Consume with Stream.foldl / Stream.tryFoldl / Stream.attemptFoldl. Prefer attemptFoldl from TEA; Stream.attempt materializes the whole file. Line failures surface as Stream.Error.

Examples

File.stream "data.csv"

streamBytes

streamBytes : Int -> String -> Stream Bytes

Lazily stream fixed-size byte chunks. The last chunk may be shorter. size less than 1 is treated as 1 for open, then yields nothing.

Examples

File.streamBytes 65536 "data.bin"

errorToString

errorToString : Error -> String

Human-readable form of an Error for logs.

Examples

File.errorToString err