Standard library · System and tools

fs/dir

Creating, listing, copying, moving and removing directories.

import fs.dir
On this page
  1. Functions

The fs.dir package covers everything you do with directories. When a function fails it records the detail in the os.error package.

Note that Exists here is true only for a directory; for files use fs.file.Exists.

Functions

Mkdir

func Mkdir(p: str): int
Creates one directory.
fs-dir-List.salam
import fs.dir
import fs.file

func main:
    dir.Mkdir("library")
    dir.MkdirAll("library/poetry/hafez")
    file.WriteText("library/index.txt", "3 books")
    file.WriteText("library/poetry/hafez/ghazal.txt", "a line of verse")
    println dir.Exists("library"), dir.IsDir("library/index.txt")
    println dir.List("library").len(), dir.ListDirs("library"), dir.ListFiles("library")
    println dir.Walk("library").len(), dir.Size("library") > 0
    println dir.Permissions("library") > 0, dir.ModTime("library") > 0
    dir.Copy("library", "backup")
    dir.Move("backup", "archive")
    println dir.Exists("backup"), dir.Exists("archive/poetry/hafez"), file.Exists("archive/poetry/hafez/ghazal.txt")
    dir.Mkdir("empty")
    dir.Remove("empty")
    dir.RemoveAll("library")
    dir.RemoveAll("archive")
    println dir.Exists("library"), dir.Exists("empty")
end
Outputtrue false 2 ["poetry"] ["index.txt"] 2 true true true false true true false false

MkdirAll

func MkdirAll(p: str): int
Creates a directory along with every parent it needs.

The example for this item is shown with the ones above: fs-dir-List.salam

Remove

func Remove(p: str): int
Removes an empty directory.

The example for this item is shown with the ones above: fs-dir-List.salam

RemoveAll

func RemoveAll(p: str): int
Removes a directory with everything in it.

The example for this item is shown with the ones above: fs-dir-List.salam

List

func List(p: str): Vector<str>
The names of everything inside a directory.

The example for this item is shown with the ones above: fs-dir-List.salam

ListDirs

func ListDirs(p: str): Vector<str>
Only the names of the subdirectories.

The example for this item is shown with the ones above: fs-dir-List.salam

ListFiles

func ListFiles(p: str): Vector<str>
Only the names of the files.

The example for this item is shown with the ones above: fs-dir-List.salam

Walk

func Walk(p: str): Vector<str>
The paths of every file in a directory and its subdirectories.

The example for this item is shown with the ones above: fs-dir-List.salam

Copy

func Copy(src: str, dst: str): int
Copies a directory with all its contents.

The example for this item is shown with the ones above: fs-dir-List.salam

Move

func Move(src: str, dst: str): int
Moves a directory; when a direct move is impossible, across two disks for instance, it copies and then removes the original.

The example for this item is shown with the ones above: fs-dir-List.salam

Exists

func Exists(p: str): bool
True when the path exists and is a directory.

The example for this item is shown with the ones above: fs-dir-List.salam

IsDir

func IsDir(p: str): bool
True when the path is a directory.

The example for this item is shown with the ones above: fs-dir-List.salam

Size

func Size(p: str): i64
The total size of every file in the directory and its subdirectories, in bytes.

The example for this item is shown with the ones above: fs-dir-List.salam

Permissions

func Permissions(p: str): int
The directory's permissions, as octal digits.

The example for this item is shown with the ones above: fs-dir-List.salam

ModTime

func ModTime(p: str): i64
When the directory was last changed.

The example for this item is shown with the ones above: fs-dir-List.salam