0

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
Compo
  • 36,585
  • 5
  • 27
  • 39
  • As you should be able to realise, if you're operating on each branch as you go, as soon as you've renamed part of the parent structure, the branch no longer exists in order to make the next change. The most efficient way to prevent that, is to somehow determine the deepest level first, and work backwards from there. That however is not a straight forward process, it will inevitably require storage and sorting of the tree structure, _(either to memory, if the tree isn't too large, or to disk)_, prior to performing any renaming. – Compo May 10 '20 at 16:20

1 Answers1

0

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.

  • call /?
  • dir /?
  • echo /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • ren /?
  • set /?
  • setlocal /?

See also: Where does GOTO :EOF return to?

Mofi
  • 46,139
  • 17
  • 80
  • 143