Windows bulk rename middle filename via CLI

batch filecommand linerenamewindows

Original Files

File 15 - Example.txt
File 2 - Example.txt
File 22 - Example.txt
File 3 - Example.txt
File 4 - Example.txt
File 5 - Example.txt

Desired Output

File 15 - Example.txt
File 02 - Example.txt
File 22 - Example.txt
File 03 - Example.txt
File 04 - Example.txt
File 05 - Example.txt

Single file can be renamed easily with ren.

ren "File 2 - Example.txt" "File 02 - Example.txt"

Would it be possible to bulk rename it with Windows ren or rename tool?

Best Answer

Would it be possible to bulk rename it with Windows ren or rename tool?

Yes, but it requires a batch file.

test.cmd:

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=1,2,3,4" %%i in ('dir /b *Example.txt') do (
  rem pad 2nd token with leading zero
  set _num=0%%j
  set _num=!_num:~-2!
  ren "%%i %%j %%k %%l" "%%i !_num! %%k %%l"
  )
endlocal

example:

> dir *Example.txt
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test\test

03/01/2019  11:30                 0 File 15 - Example.txt
03/01/2019  11:30                 0 File 2 - Example.txt
03/01/2019  11:30                 0 File 22 - Example.txt
03/01/2019  11:30                 0 File 3 - Example.txt
03/01/2019  11:30                 0 File 4 - Example.txt
03/01/2019  11:30                 0 File 5 - Example.txt
               6 File(s)              0 bytes
               0 Dir(s)  1,075,134,230,528 bytes free

> ..\test

> dir
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test\test

03/01/2019  11:54    <DIR>          .
03/01/2019  11:54    <DIR>          ..
03/01/2019  11:30                 0 File 02 - Example.txt
03/01/2019  11:30                 0 File 03 - Example.txt
03/01/2019  11:30                 0 File 04 - Example.txt
03/01/2019  11:30                 0 File 05 - Example.txt
03/01/2019  11:30                 0 File 15 - Example.txt
03/01/2019  11:30                 0 File 22 - Example.txt
               6 File(s)              0 bytes
               2 Dir(s)  1,075,134,230,528 bytes free

Further Reading

Related Question