Last active
April 1, 2023 09:09
-
-
Save skysan87/afb0c1f9ab5b3607102008670f57ace8 to your computer and use it in GitHub Desktop.
[JS] showDirectoryPicker: ルートディレクトリからファイルパスを取得
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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