Created
November 24, 2024 01:49
-
-
Save brettchalupa/7a2e24e9367c0252790cfa0f26bf06bd to your computer and use it in GitHub Desktop.
Migrate files in a directory with nested folders into a flat destination with Deno
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
import * as path from "jsr:@std/path"; | |
const src = Deno.args[0]; | |
const dest = Deno.args[1]; | |
/** | |
* Recursively crawl a directory and move the files into a dest(ination) directory (flat) | |
* Run with: `deno run -A flatten_dir.ts some_dir flat_dir | |
*/ | |
async function migrateDir(dir: string, dest: string) { | |
await Deno.mkdir(dest, { recursive: true }); | |
for await (const dirEntry of Deno.readDir(dir)) { | |
const filePath = path.join(dir, dirEntry.name); | |
console.log(filePath); | |
if (dirEntry.isDirectory) { | |
await migrateDir(filePath, dest); | |
} else { | |
const newPath = path.join(dest, path.basename(dirEntry.name)); | |
console.log(`Moving ${filePath} to ${newPath}`); | |
await Deno.rename( | |
filePath, | |
newPath, | |
); | |
} | |
} | |
} | |
await migrateDir(src, dest); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment