Skip to content

Instantly share code, notes, and snippets.

@hashrock
Created November 26, 2022 11:07
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 hashrock/83d144f0adea4fdbe28de57c16dd173d to your computer and use it in GitHub Desktop.
Save hashrock/83d144f0adea4fdbe28de57c16dd173d to your computer and use it in GitHub Desktop.
Deno pack file to yyyyMMdd - dir
import {format} from "https://deno.land/std@0.166.0/datetime/mod.ts";
import {basename, dirname} from "https://deno.land/std@0.166.0/path/mod.ts";
const args = Deno.args;
if (args.length === 0) {
console.log("No arguments provided");
Deno.exit(1);
}
await moveToYmdDir();
async function moveToYmdDir() {
const file = args[0];
const filePath = Deno.realPathSync(file);
const fileInfo = await Deno.stat(filePath);
if (fileInfo.birthtime === null) {
console.log("File read error");
Deno.exit(1);
}
const date = new Date(fileInfo.birthtime);
const ymd = format(date, "yyyyMMdd");
const parent = dirname(filePath);
const base = basename(filePath).split(".")[0];
const baseWithExt = basename(filePath);
const newDirName = `${parent}/${ymd}-${base}`;
await Deno.mkdir(newDirName, { recursive: true });
const newFile = `${newDirName}/${baseWithExt}`;
await Deno.rename(filePath, newFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment