Solved Renaming files add characters when only 2 numbers

September 29, 2012 at 05:28:07
Specs: Linux/VMWare
I have a batch script to rename files but in some cases I will have only 2 numbers on the original file name but I want to rename it with 4 numbers so, filling in with 2 zero's.

i.e.: Input: Record_89
Output: Record_0089

The problem is that I can have files with only 2 numbers (as above) and some with 4 numbers, in this case I don't want to change it.

i.e.: Input: Record_3207
Output: Record_3207

I think I would need to use an IF to only rename the files with 2 characters at the end of its name, I know it is very simple but I am not sure how to do it.

I am renaming the files on the way below (for the 2 characters files... I will need an IF to make a condition... ??

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

Many thanks! :D

See More: Renaming files add characters when only 2 numbers

Report •

September 29, 2012 at 19:36:29
✔ Best Answer
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1-3 delims=_." %%a in ('dir /b *.png') do (
set x=0000%%b
set x=!x:~-4!
echo ren %%a_%%b.%%c %%a_!x!.%%c
::===== end script
can't take credit, not my idea, but it is very good method.

Report •

September 30, 2012 at 02:52:36
Thank you very much... very simple and useful!! :D

Report •

Related Solutions

Ask Question