Function walk

  • Recursively walks through a directory and yields information about each file and directory encountered.

    The root path determines whether the file paths are relative or absolute. The root directory is included in the yielded entries.

    Requires --allow-read permission.

    Parameters

    • root: string | URL

      The root directory to start the walk from, as a string or URL.

    • Optionaloptions: WalkOptions

      The options for the walk.

    Returns AsyncIterableIterator<WalkEntry>

    An async iterable iterator that yields the walk entry objects.

    https://docs.deno.com/runtime/manual/basics/permissions#file-system-access for more information on Deno's permissions system.

    If the root directory does not exist.

    File structure:

    folder
    ├── script.ts
    └── foo.ts
    import { walk } from "@std/fs/walk";

    await Array.fromAsync(walk("."));
    // [
    // {
    // path: ".",
    // name: ".",
    // isFile: false,
    // isDirectory: true,
    // isSymlink: false
    // },
    // {
    // path: "script.ts",
    // name: "script.ts",
    // isFile: true,
    // isDirectory: false,
    // isSymlink: false
    // },
    // {
    // path: "foo.ts",
    // name: "foo.ts",
    // isFile: true,
    // isDirectory: false,
    // isSymlink: false
    // },
    // ]

    Setting the maxDepth option to 1 will only include the root directory and its immediate children.

    File structure:

    folder
    ├── script.ts
    └── foo
    └── bar.ts
    import { walk } from "@std/fs/walk";

    await Array.fromAsync(walk(".", { maxDepth: 1 }));
    // [
    // {
    // path: ".",
    // name: ".",
    // isFile: false,
    // isDirectory: true,
    // isSymlink: false
    // },
    // {
    // path: "script.ts",
    // name: "script.ts",
    // isFile: true,
    // isDirectory: false,
    // isSymlink: false
    // },
    // {
    // path: "foo",
    // name: "foo",
    // isFile: false,
    // isDirectory: true,
    // isSymlink: false
    // },
    // ]

    Setting the includeFiles option to false will exclude files.

    File structure:

    folder
    ├── script.ts
    └── foo
    import { walk } from "@std/fs/walk";

    await Array.fromAsync(walk(".", { includeFiles: false }));
    // [
    // {
    // path: ".",
    // name: ".",
    // isFile: false,
    // isDirectory: true,
    // isSymlink: false
    // },
    // {
    // path: "foo",
    // name: "foo",
    // isFile: false,
    // isDirectory: true,
    // isSymlink: false,
    // },
    // ]

    Setting the includeDirs option to false will exclude directories.

    File structure:

    folder
    ├── script.ts
    └── foo
    import { walk } from "@std/fs/walk";

    await Array.fromAsync(walk(".", { includeDirs: false }));
    // [
    // {
    // path: "script.ts",
    // name: "script.ts",
    // isFile: true,
    // isDirectory: false,
    // isSymlink: false
    // },
    // ]

    Setting the exts option to [".ts"] or ["ts"] will only include entries with the .ts file extension.

    File structure:

    folder
    ├── script.ts
    └── foo.js
    import { walk } from "@std/fs/walk";

    await Array.fromAsync(walk(".", { exts: [".ts"] }));
    // [
    // {
    // path: "script.ts",
    // name: "script.ts",
    // isFile: true,
    // isDirectory: false,
    // isSymlink: false
    // },
    // ]

    Setting the match option to [/.s/] will only include entries with the letter s in their name.

    File structure:

    folder
    ├── script.ts
    └── README.md
    import { walk } from "@std/fs/walk";

    await Array.fromAsync(walk(".", { match: [/s/] }));
    // [
    // {
    // path: "script.ts",
    // name: "script.ts",
    // isFile: true,
    // isDirectory: false,
    // isSymlink: false
    // },
    // ]

    Setting the skip option to [/.s/] will exclude entries with the letter s in their name.

    File structure:

    folder
    ├── script.ts
    └── README.md
    import { walk } from "@std/fs/walk";

    await Array.fromAsync(walk(".", { skip: [/s/] }));
    // [
    // {
    // path: "README.md",
    // name: "README.md",
    // isFile: true,
    // isDirectory: false,
    // isSymlink: false
    // },
    // ]