← Back to lessons

36 Directories

Hard

There is a lot of different functions available in Cangjie regarding directories. We only cover a small portion of them here. You can find the rest here

Open current directory.

Check if subDirectory “Max_is_amazing” exists.

If it does not exist, create it.

If it does, open it.

Create a file in the subDirectory.

Go over all directories and files strictly under current directory. (non recursive)

Open parent directory. Iterate over all subDirectories of the parent directory.

Directories.cj
import std.fs.*

main() {
    var currentDirectory = Directory(Path("./"))

    var subDir: Directory
    if (Directory.exists(Path("./").join("Max_is_amazing")) == false) {
        subDir = currentDirectory.createSubDirectory("Max_is_amazing")
    } else {
        subDir = Directory(Path("./").join("Max_is_amazing"))
    }

    subDir.createFile("Max_Isaac_Ali.txt")

    for (i in currentDirectory.entryList()) {
        println(i.path)
    }

    println()

    var parentDirectory = Directory(Path("../"))
    for (i in parentDirectory.directoryList()) {
        println(i.path)
    }
}