Created
November 26, 2022 11:07
-
-
Save hashrock/83d144f0adea4fdbe28de57c16dd173d to your computer and use it in GitHub Desktop.
Deno pack file to yyyyMMdd - dir
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 {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