Hello from MCP server

List Files | Just Commands | Repo | Logs

← back |
const fs = require("fs");
const path = require("path");

function renameFilesInSteps(dirPath, step = 10) {
  const prefixRegex = /^(\d{10})_(.+)$/;

  const files = fs
    .readdirSync(dirPath)
    .map((filename) => {
      const match = filename.match(prefixRegex);
      return match
        ? { ts: parseInt(match[1]), name: filename, rest: match[2] }
        : null;
    })
    .filter(Boolean)
    .sort((a, b) => a.ts - b.ts);

  files.forEach((file, index) => {
    const newPrefix = String((index + 1) * step).padStart(10, "0");
    const newName = `${newPrefix}_${file.rest}`;
    const oldPath = path.join(dirPath, file.name);
    const newPath = path.join(dirPath, newName);
    fs.renameSync(oldPath, newPath);
    console.log(`Renamed: ${file.name} → ${newName}`);
  });
}

// Example usage
renameFilesInSteps("./pb_migrations", 10);