简体   繁体   中英

Batch Rename Subdirectories in Windows

I have a directory with thousands of subdirectories that contain their own subdirectories that need to be renamed. I'm using a Windows 7 machine that I do not have Administrator rights for so I can't download a simple program to do this for me.

Right now I have a test directory C:\\test with a few subdirectories that have subdirectories named old that I am trying to change to new using a batch file.

Just to be clear I want the following:

C:\test\1\old
C:\test\2\old
C:\test\3\old

to become

C:\test\1\new
C:\test\2\new
C:\test\3\new

Thank you for any help you can provide.

Justin's answer was really helpful for my similar problem, though by default it only handled a simpler pattern for \\A\\B\\C where A is a base directory, B is some undetermined directory and C is the directory you seek.

I modifed his script to recurse the base directory A through any number of layers until it finds C.

Here's the script, written to expect command line paramers:

@echo off

set BASEDIR=%1
SET CRITERIA=\%2
SET REPLACENAME=%3

call :FindDirs %BASEDIR%

GOTO END

:FindDirs
FOR /D %%F IN ("%1\*") DO CALL :RENAME %%F
GOTO:EOF

:RENAME
REM echo DIR=%1
FOR /D %%R IN ("%1%CRITERIA%") DO (
    if EXIST %%R RENAME %%R "%REPLACENAME%"
)
call :FindDirs %1
GOTO:EOF

Here's what I came up with quickly. I ran a quick test locally and it seemed to do what you're asking for:

@echo off

FOR /D %%D IN ("C:\test\*") DO CALL :RENAME %%D

:RENAME
SET CRITERIA=\old
FOR /D %%R IN (%1%CRITERIA%) DO RENAME %%R "new"

Save that to a bat file and give it a shot. Hopefully that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM