Last updated
Batch File Renaming Strategies
Batch file renaming applies a consistent transformation to multiple filenames at once. Common operations include adding prefixes/suffixes, replacing text, changing case, adding sequential numbers, and changing file extensions. This is essential for organizing photo libraries, normalizing asset names, and preparing files for upload to systems with naming conventions.
Renaming Patterns
| Operation | Before | After |
|---|---|---|
| Add prefix | photo.jpg | 2026_photo.jpg |
| Add sequence | img.jpg | img_001.jpg |
| Replace text | IMG_1234.jpg | vacation_1234.jpg |
| Lowercase | MyFile.TXT | myfile.txt |
| Remove spaces | my file.pdf | my_file.pdf |
| Change extension | image.jpeg | image.jpg |
Batch Rename with Node.js
import { readdir, rename } from 'fs/promises';
import path from 'path';
async function batchRename(dir, transform) {
const files = await readdir(dir);
let count = 0;
for (const [i, file] of files.entries()) {
const newName = transform(file, i);
if (newName !== file) {
await rename(
path.join(dir, file),
path.join(dir, newName)
);
console.log(`${file} → ${newName}`);
count++;
}
}
console.log(`Renamed ${count} files`);
}
// Add sequential prefix
batchRename('./photos', (name, i) =>
`${String(i+1).padStart(3,'0')}_${name}`
);
// Replace spaces with underscores, lowercase
batchRename('./docs', name =>
name.toLowerCase().replace(/\s+/g, '_')
);