Skip to content

Instantly share code, notes, and snippets.

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 loicdescotte/f0295a086cf0fa5cbd7131b730cab71f to your computer and use it in GitHub Desktop.
Save loicdescotte/f0295a086cf0fa5cbd7131b730cab71f to your computer and use it in GitHub Desktop.
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