Skip to content

Instantly share code, notes, and snippets.

@htunnicliff
Created January 16, 2022 02:52
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 htunnicliff/8bc66d80a21415931525955a2d2b1131 to your computer and use it in GitHub Desktop.
Save htunnicliff/8bc66d80a21415931525955a2d2b1131 to your computer and use it in GitHub Desktop.

GitHub Copilot Miracles

Whenever Copilot writes a script that does exactly what I was hoping it to do, I will record them here.

Rename all movies in a directory from "YYYY - Title.ext" to "Title (YYYY).ext"

import fs from "fs/promises";

async function renameMovies(dir) {
  const files = await fs.readdir(dir);
  for (const file of files) {
    const match = /^(\d{4}) - (.*)\.\w+$/.exec(file);
    if (match) {
      const [, year, title] = match;
      const newName = `${title} (${year}).${file.split(".").pop()}`;
      await fs.rename(`${dir}/${file}`, `${dir}/${newName}`);
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment