Deno logoDenoby example

Beginner

cli

Walking directories

When doing something like filesystem routing, it is useful to be able to walk down a directory to visit files.

If the directory has no depth (no folders), we can use the built-in Deno.readDir
for await (const dirEntry of Deno.readDir(".")) {
  console.log("Basic listing:", dirEntry.name);
}
If on the other hand you need to recursively walk a repository, the standard library has a method for this. In the most simple case it is a drop-in replacement
import { walk } from "https://deno.land/std@0.175.0/fs/walk.ts";

for await (const dirEntry of walk(".")) {
  console.log("Recursive walking:", dirEntry.name);
}
We are also able to specify some settings to customize our results. In the case of building filesystem routing limiting results to only certain extensions may be useful
for await (const dirEntry of walk(".", { exts: ["ts"] })) {
  console.log("Recursive walking with extension:", dirEntry.name);
}

Run this example locally using the Deno CLI:

deno run --allow-read https://byexample-wwv03xf36j0g.deno.dev/walking-directories.ts

Additional resources: