Skip to content

Instantly share code, notes, and snippets.

@skysan87
Last active April 1, 2023 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skysan87/afb0c1f9ab5b3607102008670f57ace8 to your computer and use it in GitHub Desktop.
Save skysan87/afb0c1f9ab5b3607102008670f57ace8 to your computer and use it in GitHub Desktop.
[JS] showDirectoryPicker: ルートディレクトリからファイルパスを取得
// @see: https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle
async function* load (dirHandle, dir = '') {
const currentdir = dir + '/' + dirHandle.name;
for await (const handle of dirHandle.values()) {
if (handle.kind === 'file') {
yield currentdir + '/' + handle.name;
} else if (handle.kind === 'directory') {
yield* load(handle, currentdir);
}
}
}
// Usage
async function readDirectory() {
const dirHandle = await showDirectoryPicker({ mode: 'read' });
if (!dirHandle) return;
for await (const filename of load(dirHandle)) {
console.log(filename); // /root/sub/filename.ext
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment