Software >> OS >> Windows >> Command Line >> How to rename files matching a pattern



## Example 1 : To move and rename files x_y_z.txt to x.txt
 
on command line:
FOR /F "usebackq tokens=1,2* delims=_" %i in (`DIR /B original\*.TXT`) do move original\%i_%j_%k renamed\%i.txt
 
on batch files:
FOR /F "usebackq tokens=1,2* delims=_" %%i in (`DIR /B original\*.TXT`) do move original\%%i_%%j_%%k renamed\%%i.txt



## Example 2: To rename files a_b.c to a.c

on command line:
FOR /F "usebackq tokens=1,2,3 delims=_." %a in (`DIR /b *_*.*`) do rename %a_%b.%c %a.%c

in batch file:
FOR /F "usebackq tokens=1,2,3 delims=_." %%a in (`DIR /b *_*.*`) do rename %%a_%%b.%%c %%a.%%c