You are not logged in.

#1 20 Sep 2019 10:08

PiotrMP006
Member
Registered: 02 Apr 2019
Posts: 12

How do I recursively rename directories using a batch script?

I have a folder tree like this, (it's only an example).

C:\TEST
├───Test1
│   └───Test
├───Test2
│   └───Test
└───Test3
    └───Test

How do I recursively rename all Test directories using a batch-file?

Offline

#2 23 Sep 2019 08:00

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: How do I recursively rename directories using a batch script?

There are a few ways to skin this cat.

Here's one which I think would work (not tested):

for /f "usebackq tokens=*" %%D in (`dir /a:d/b/s "C:\TEST" ^| findstr /l/i/e "\\Test"`) do rename "%%~D" "%%~dpD\NewName"

EDIT: Oh, in the example you've given I think that will probably rename the top level directory, because it has the same name as the directory you are searching for.

This, I think, should make sure you only match on a sub-directory that is not your top level (again, untested):

set TOPLEVEL="C:\TEST"
set MATCH="Test"
for /f "usebackq tokens=*" %%D in (`dir /a:d/b/s "%TOPLEVEL%" ^| findstr /l/i/e "%TOPLEVEL:\=\\%\\.*\\Test"`) do (
  rename "%%~D" "%%~dpD\NewName"
)

Last edited by bluesxman (23 Sep 2019 09:09)


cmd | *sh | ruby | chef

Offline

Board footer

Powered by