Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename multiple folders with pattern in name using Batch

I have 100 folders under a directory with naming in a pattern. Ex: DeDeP001M1TSub, DeDeP002M1TSub,...,DeDeP100M1TSub.

I am looking for a command line option to rename all the folders. I just want to change M1 to M2 as in DeDeP001M1TSub becomes DeDeP001M2TSub. I know of 3rd party applications that can do this, but am looking for a command line option to use in a bat file along with bunch of other stuff. Please help(Fairly new to the area)!!

So far have tried ren,mv but to no effect(not very familiar with dos scripting).

like image 418
Matt Avatar asked Feb 03 '26 18:02

Matt


1 Answers

This should do it:

@echo off
setlocal enabledelayedexpansion

for /d %%a in (*) do (
  set "p=%%a"
  set "fp=!p:~0,8!" & set "tp=!p:~10!"
  echo ren %%a !fp!M2!tp!
)

Remove the echo once you verify the output is what you want to do the actual rename.

like image 131
Matt Williamson Avatar answered Feb 05 '26 09:02

Matt Williamson