batch-filedirectoryrenamelevels

Rename at least five levels of subdirectories within a directory?


I have following input path: C:\Temp\01 - one\02 - two\03 - three\04 - four\05 - five, which I wish to rename to C:\Temp\01 one\02 two\03 three\04 four\05 five

The following batch renames 2 levels, but I wish to rename at least 5 levels:

@echo off
setlocal enabledelayedexpansion
pushd "C:\Temp"

for /d %%z in (*) do (
  pushd "%%z"
     for /d %%a in (*) do (
         set "folder=%%a"
         ren "%%a" "!folder:-=!"
     )
  popd
)    
for /d %%a in (*) do (
     set "folder=%%a"
     ren "%%a" "!folder:-=!"
)
popd

Solution

  • Such a recursive folder rename can be done using a subroutine which calls itself recursively as often as need which means in this case calling itself as long as a subfolder is found with a hyphen between two spaces.

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    for /F "eol=| delims=" %%I in ('dir "* - *" /AD /B 2^>nul') do call :RenameFolder "%%~fI"
    exit /B
    
    :RenameFolder
    for /F "eol=| delims=" %%I in ('dir "%~1\* - *" /AD /B 2^>nul') do call :RenameFolder "%~1\%%I"
    set "NewFolderName=%~nx1"
    set "NewFolderName=%NewFolderName:-=%"
    ren %1 "%NewFolderName%"
    goto :EOF
    

    The important trick here is that each FOR loop processes a captured list of folder names in memory and do not process the folder names matching the wildcard pattern directly from file system as the list of folder names matching the wildcard pattern changes on each rename of a folder. The result would be undefined on processing a list of folder names which changes on each folder rename.

    This batch file ignores a folder not containing space, hyphen, space in name and all its subfolders even on a subfolder containing space, hyphen, space in name. For that reason it works as efficient as possible for the folder example C:\Temp\01 - one\02 - two\03 - three\04 - four\05 - five on running the batch file with C:\Temp being the current directory.


    Here is one more solution which processes really all directories in current directory and all its subdirectories. There are renamed all subdirectories containing space, hyphen, space in directory name.

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    for /F "eol=| delims=" %%I in ('dir /AD /B 2^>nul') do call :ProcessFolder "%%~fI"
    exit /B
    
    :ProcessFolder
    for /F "eol=| delims=" %%I in ('dir "%~1\" /AD /B 2^>nul') do call :ProcessFolder "%~1\%%I"
    set "NewFolderName=%~nx1"
    set "NewFolderName=%NewFolderName: - =  %"
    if not "%~nx1" == "%NewFolderName%" ren %1 "%NewFolderName%"
    goto :EOF
    

    This batch file works also for C:\Temp\ABCD Training company Ltd\500020 - Test\0500020011 - Test with C:\Temp being the current directory although ABCD Training company Ltd does not contain space, hyphen, space in its name.


    To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

    See also: Where does GOTO :EOF return to?