Solved Rename file names in batch

September 29, 2012 at 10:11:39
Specs: Linux/VMWare
I have a batch script to rename files and at the moment I have files:


So the same prefix but with different numbers of characters after the underscore (_).

I would like to rename these files to have all of them with the same number of characters (4 characters), as:


So I am not sure how to make the script detects when the file name has less than 4 characters after the underscore (_) and fill it with zeros so all files with the same length.

I am renaming the files on the way below (for the 2 characters files...)

for %%j in (Record*.png) do (
set iname=!iname:~7,2!
ren "%%j" "Record_00!iname!.png"

Many thanks! :D

See More: Rename file names in batch

Report •

October 2, 2012 at 13:17:34
✔ Best Answer
Remove the echo to actually do the rename.

@echo off & setlocal enabledelayedexpansion
set pad=000
for /f "tokens=1,2 delims=_.txt" %%a in (
'dir /b *.txt'
) do (
set fname=%%a
set num=%%b&&set num=%pad%!num!&&set num=!num:~-4!
echo move %%a_%%b.txt !fname!_!num!.txt


Report •

October 3, 2012 at 02:36:10
Thank you very much!! :D

Report •

Related Solutions

Ask Question