How to rename file by replacing substring using batch in Windows [closed]

@echo off

Set "Filename=how-to-rename-file.jpg"
Set "Pattern=rename"
Set "Replace=reuse"

REM Call Rename "%Filename%" "%%Filename:%Pattern%=%Replace%%%"

Call Echo %%Filename:%Pattern%=%Replace%%%
:: Result: how-to-reuse-file.jpg

Pause&Exit

I give you other example for a loop of files:

UPDATE:

I’ve missed some things in the syntax ’cause fast-typing my last edit, here is the corrected code:

@echo off
Setlocal enabledelayedexpansion

Set "Pattern=rename"
Set "Replace=reuse"

For %%# in ("C:\Folder\*.jpg") Do (
    Set "File=%%~nx#"
    Ren "%%#" "!File:%Pattern%=%Replace%!"
)

Pause&Exit

PS: You can read here to learn more about substring:
http://ss64.com/nt/syntax-substring.html
http://ss64.com/nt/syntax-replace.html

Leave a Comment