-
-
Save loicdescotte/f0295a086cf0fa5cbd7131b730cab71f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const config = { | |
typeDirs: [ | |
{ type: "pdf", directory: "documents" }, | |
{ type: "png", directory: "images" }, | |
{ type: "mp3", directory: "music" }, | |
], | |
}; | |
type Config = typeof config; | |
type TypeDirs = Config["typeDirs"]; | |
const DIRECTORY_NAME = Deno.args[0]; | |
if (DIRECTORY_NAME) { | |
createCategoriesDirectories(config.typeDirs, DIRECTORY_NAME); | |
moveFiles(DIRECTORY_NAME); | |
} else { | |
console.log("Specify target directory"); | |
} | |
function createCategoriesDirectories( | |
typeDirs: TypeDirs, | |
directoryName: string | |
) { | |
[...typeDirs, { directory: "other" }].map((d) => { | |
const dirname = `${directoryName}/${d.directory}`; | |
Deno.mkdirSync(dirname); | |
}); | |
} | |
async function moveFiles(directoryName: string) { | |
for await (const file of Deno.readDir(directoryName)) { | |
const extname = file.name.split(".")[1] | |
if (extname) { | |
const { directory: targetDir = "other" } = | |
config.typeDirs.find((dir) => dir.type == extname) || {}; | |
const oldPath = `${Deno.cwd()}/${directoryName}/${file.name}`; | |
const newPath = `${Deno.cwd()}/${directoryName}/${targetDir}/${file.name}`; | |
Deno.renameSync(oldPath, newPath); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment