Function walkSync
- walk
Sync (root, options?): IterableIterator<WalkEntry> Parameters
- root: string | URL
The root directory to start the walk from, as a string or URL.
Optionaloptions: WalkOptionsThe options for the walk.
Returns IterableIterator<WalkEntry>
A synchronous iterable iterator that yields the walk entry objects.
See
https://docs.deno.com/runtime/manual/basics/permissions#file-system-access for more information on Deno's permissions system.
Example: Basic usage
File structure:
folder
├── script.ts
└── foo.tsimport { walkSync } from "@std/fs/walk";
Array.from(walkSync("."));
// [
// {
// 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
// },
// ]Example: Maximum file depth
Setting the
maxDepthoption to1will only include the root directory and its immediate children.File structure:
folder
├── script.ts
└── foo
└── bar.tsimport { walkSync } from "@std/fs/walk";
Array.from(walkSync(".", { 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
// },
// ]Example: Exclude files
Setting the
includeFilesoption tofalsewill exclude files.File structure:
folder
├── script.ts
└── fooimport { walkSync } from "@std/fs/walk";
Array.from(walkSync(".", { includeFiles: false }));
// [
// {
// path: ".",
// name: ".",
// isFile: false,
// isDirectory: true,
// isSymlink: false
// },
// {
// path: "foo",
// name: "foo",
// isFile: false,
// isDirectory: true,
// isSymlink: false,
// },
// ]Example: Exclude directories
Setting the
includeDirsoption tofalsewill exclude directories.File structure:
folder
├── script.ts
└── fooimport { walkSync } from "@std/fs/walk";
Array.from(walkSync(".", { includeDirs: false }));
// [
// {
// path: "script.ts",
// name: "script.ts",
// isFile: true,
// isDirectory: false,
// isSymlink: false
// },
// ]Example: Exclude symbolic links
Setting the
includeSymlinksoption tofalsewill exclude symbolic links.File structure:
folder
├── script.ts
├── foo
└── link -> script.ts (symbolic link)import { walkSync } from "@std/fs/walk";
Array.from(walkSync(".", { includeSymlinks: false }));
// [
// {
// path: ".",
// name: ".",
// isFile: false,
// isDirectory: true,
// isSymlink: false
// },
// {
// path: "script.ts",
// name: "script.ts",
// isFile: true,
// isDirectory: false,
// isSymlink: false
// },
// ]Example: Follow symbolic links
Setting the
followSymlinksoption totruewill follow symbolic links, affecting thepathproperty of the walk entry.File structure:
folder
├── script.ts
└── link -> script.ts (symbolic link)import { walkSync } from "@std/fs/walk";
Array.from(walkSync(".", { followSymlinks: true }));
// [
// {
// path: ".",
// name: ".",
// isFile: false,
// isDirectory: true,
// isSymlink: false
// },
// {
// path: "script.ts",
// name: "script.ts",
// isFile: true,
// isDirectory: false,
// isSymlink: false
// },
// {
// path: "script.ts",
// name: "link",
// isFile: true,
// isDirectory: false,
// isSymlink: true
// },
// ]Example: Canonicalize symbolic links
Setting the
canonicalizeoption tofalsewill canonicalize the path of the followed symbolic link. Meaning, thepathproperty of the walk entry will be the path of the symbolic link itself.File structure:
folder
├── script.ts
└── link -> script.ts (symbolic link)import { walkSync } from "@std/fs/walk";
Array.from(walkSync(".", { followSymlinks: true, canonicalize: true }));
// [
// {
// path: ".",
// name: ".",
// isFile: false,
// isDirectory: true,
// isSymlink: false
// },
// {
// path: "script.ts",
// name: "script.ts",
// isFile: true,
// isDirectory: false,
// isSymlink: false
// },
// {
// path: "link",
// name: "link",
// isFile: true,
// isDirectory: false,
// isSymlink: true
// },
// ]Example: Filter by file extensions
Setting the
extsoption to[".ts"]or["ts"]will only include entries with the.tsfile extension.File structure:
folder
├── script.ts
└── foo.jsimport { walkSync } from "@std/fs/walk";
Array.from(walkSync(".", { exts: [".ts"] }));
// [
// {
// path: "script.ts",
// name: "script.ts",
// isFile: true,
// isDirectory: false,
// isSymlink: false
// },
// ]Example: Filter by regular expressions
Setting the
matchoption to[/.s/]will only include entries with the lettersin their name.File structure:
folder
├── script.ts
└── README.mdimport { walkSync } from "@std/fs/walk";
Array.from(walkSync(".", { match: [/s/] }));
// [
// {
// path: "script.ts",
// name: "script.ts",
// isFile: true,
// isDirectory: false,
// isSymlink: false
// },
// ]Example: Exclude by regular expressions
Setting the
skipoption to[/.s/]will exclude entries with the lettersin their name.File structure:
folder
├── script.ts
└── README.mdimport { walkSync } from "@std/fs/walk";
Array.from(walkSync(".", { skip: [/s/] }));
// [
// {
// path: "README.md",
// name: "README.md",
// isFile: true,
// isDirectory: false,
// isSymlink: false
// },
// ]- root: string | URL
Recursively walks through a directory and yields information about each file and directory encountered.
The root path determines whether the file paths is relative or absolute. The root directory is included in the yielded entries.
Requires
--allow-readpermission.