VECHIN
VECHIN

Reputation: 13

Batch file to rename files in multiple folders

I have the folder and file structure as given below. I am in need of a MS DOS batch file to rename the files in multiple folders. Can anyone please help out?

- Main Folder

    -->Sub Folder1
        --- File1_EN.txt
        --- File2_EN.txt

    --> Sub Folder2
        --- File3_EN.txt
        --- File4_EN.txt

I want to rename the suffix "EN" in file names to "ENU".

Upvotes: 1

Views: 11026

Answers (4)

SbnSeebee
SbnSeebee

Reputation: 64

If you want all child folders to be changed use:

for /f "delims=*" %a in ('dir File*_EN.txt /b /s') do ren "%a" File*_ENU.txt

Upvotes: 0

Aacini
Aacini

Reputation: 67206

@echo off
for /D %%d in (*) do (
   ren "%%d\File*_EN.txt" "File*_ENU.txt"
)

Upvotes: 1

Endoro
Endoro

Reputation: 37569

Try this:

ren folder1\file*.txt file*_enu.txt
ren folder2\file*.txt file*_enu.txt

Upvotes: 0

ElektroStudios
ElektroStudios

Reputation: 20464

You can do it by this way:

@Echo OFF

Set "Folder=C:\Users\Administrador\Desktop\Nueva carpeta"
Set "Suffix=_EN"
Set "Replace=_ENU"
Set "RegEx=\".*%Suffix%\"$" 

FOR /R "%Folder%" %%# in ("*") DO (
    (Echo "%%~n#"| FINDSTR /I "%RegEx%" 1>NUL) && (
    Set "NewFileName=%%~nx#"
    Call Set "NewFileName=%%NewFileName:%Suffix%=%Replace%%%"
    Call Echo [+] Renaming: "%%~nx#" "%%NewFileName%%"
    Ren "%%#" "%%NewFileName%%"
    )
)

Pause&Exit

The Findstr is to ensure the matched string is a suffix, is better than doing a substring or splitting the filename from "_" character to the right.

Upvotes: 0

Related Questions