Skip to content

File.Entry ​

ant
type File.Entry = { name: string, isDir: bool, isSymlink: bool }

File.Entry is one entry of a directory, as File.readDir reports it. name is the entry's name, without the directory. isDir and isSymlink describe the entry itself and not what a link points to: a symbolic link has isSymlink true and isDir false, also when it points to a directory, and File.stat of its path tells what it points to.

Details ​

  • name is the entry's name alone, as File.list gives it. Join it to the directory with Path.join to get a path.
  • isDir and isSymlink are never both true: a symbolic link is a link, whatever it points to.
  • A File.Entry has no size, time, or mode; File.stat has those, as a File.Info.

Basic examples ​

Get the entries of a directory with File.readDir and read their fields:

ant
for entry in try File.readDir("notes") {
  println("${entry.name} ${entry.isDir} ${entry.isSymlink}")
}
text
archive true false
todo.txt false false

Scope ​

A File.Entry is a record, so it destructures and compares as one:

ant
let entry = (try File.readDir("inbox"))[0]
let { name, isDir } = entry
println(name)
println(isDir)
println(entry == File.Entry{ name: "todo.txt", isDir: false, isSymlink: false })
text
todo.txt
false
true

A File.Entry prints as a record:

ant
println((try File.readDir("shelf"))[0])
text
File.Entry{ name: "archive", isDir: true, isSymlink: false }

Properties and relations ​

A File.Entry holds the three fields of the same names that a File.Info has when File.stat is told not to follow a link:

ant
try File.symlink("archive", "desk/old")
for entry in try File.readDir("desk") {
  let info = try File.stat(Path.join(["desk", entry.name]), follow: false)
  assert(entry.name == info.name)
  assert(entry.isDir == info.isDir)
  assert(entry.isSymlink == info.isSymlink)
}

Possible issues ​

isDir is false for a link to a directory. Ask File.stat, which follows the link, what it points to:

ant
try File.mkdir("shortcuts/sub")
try File.symlink("sub", "shortcuts/to-dir")
let entry = (try File.readDir("shortcuts")).find({ x.name == "to-dir" })
println(entry?.isDir)
println((try File.stat("shortcuts/to-dir")).isDir)
text
false
true

name is not a path. Join it to the directory with Path.join before you read or stat the entry, as the same issue on File.readDir shows.

See also ​