Sequence NODE_230
Medium

Rename All Files in a Folder with Prefix

Node.js
Technical Specification

Write a script that adds a prefix to all file names in a given folder.

Input/Output Samples
Input:node rename.js ./images img_
Output:image1.png -> img_image1.png
Optimal Logic Path
const fs = require("fs");
const path = require("path");

const dir = process.argv[2];
const prefix = process.argv[3] || "prefix_";

fs.readdirSync(dir).forEach((name) => {
  const oldPath = path.join(dir, name);
  const stat = fs.statSync(oldPath);
  if (!stat.isFile()) return;
  const newPath = path.join(dir, prefix + name);
  fs.renameSync(oldPath, newPath);
});
Architectural Deep-Dive
We loop over all entries, filter only files, and rename them with the desired prefix.