Skip to content

Instantly share code, notes, and snippets.

@jswhisperer
Created February 11, 2025 22:54
Show Gist options
  • Save jswhisperer/80496059583d48d895f5a5c0ae77acf3 to your computer and use it in GitHub Desktop.
Save jswhisperer/80496059583d48d895f5a5c0ae77acf3 to your computer and use it in GitHub Desktop.
listDirectoryContents = async (directoryHandle, depth) => {
depth = depth || 1;
directoryHandle = directoryHandle || await navigator.storage.getDirectory();
const entries = await directoryHandle.values();
for await (const entry of entries) {
// Add proper indentation based on the depth
const indentation = ' '.repeat(depth);
if (entry.kind === 'directory') {
// If it's a directory, log its name
// and recursively list its contents
console.log(`${indentation}${entry.name}/`);
await listDirectoryContents(entry, depth + 1);
} else {
// If it's a file, log its name
console.log(`${indentation}${entry.name}`);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment