Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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