// Name: Batch Rename Files // Description: Renames files in a directory using a template. // Author: JosXa // 1. Get the directory from the user. const directory = await path({ onlyDirs: true, placeholder: 'Pick a directory to rename files in', }); // 2. Get the template for the new file names. const templateString = await template( 'NewName_$1', { placeholder: 'Enter the file name template (use $1, $2... for sequence)', }, ); // 3. Read the files from the directory. const files = await readdir(directory); // 4. Construct final paths. const operations = files.map(async (file, index) => { const newName = templateString.replace('$1', String(index + 1)); // TODO: Support $2, $3 etc. const oldPath = path.join(directory, file); const newPath = path.join(directory, newName + path.extname(file)); return { oldPath, newPath, file }; }); // 5. Perform renames. for (const operationProm of operations) { const { newPath, oldPath, file } = await operationProm; try { await move(oldPath, newPath); console.log(`Renamed "${file}" to "${path.basename(newPath)}"`); } catch (error) { console.error(`Failed to rename "${file}":`, error); } } // 6. Notify user. notify({ title: 'Batch Rename Complete', message: `Renamed ${files.length} files in ${path.basename(directory)}`, });