Home > other >  Renaming Folders using batch
Renaming Folders using batch

Time:01-13

so this has been puzzling me
I need to help to make this ('Dir /D /S A:D')
work with ren "New Folder" New Files"

I have tried different ways, but I just can't get the folders renamed
First attempt
@ECHO OFF

FOR /D %%d IN ('Dir /D /S A:D') DO (
       REN "New Folder" "New Files"
)

Second attempt

@echo off

FOR /D %%D IN ('Dir /D /S A:D') DO RENAME "%%D\New Folder" New Files

Third attempt

@echo off

FOR /D %%D IN ('Dir /D /S A:D') DO CALL :RENAME %%D

:RENAME
SET CRITERIA=\New Folder
FOR /D %%R IN (%1%CRITERIA%) DO RENAME %%R "New Files"

what I want to do is rename folders. I have many Folders and Subfolders
and every main Directory folder has different names so the script needs to read past every folder and find New Folder and rename it to New Files

layout
Main Folder
---- Folder 1
------| New Folder
---- Folder 2
------| Folder 22
----------| New Folder

New folder is found in many different locations so the script needs to be able to read main folders, sub folders, and so on

any help with this will great

Updated Question

@echo off

for /f "tokens=*" %%f in ('dir /s /b a:d "New Folder"') do (
    set "filename=%%~nf"
    ren "%%f" "New Files"
)

Before

Main Folder...
Rename folder.bat
....| New Folder
......| New Folder
........| New Folder

Bad RESULTS

Main Folder...
Rename folder.bat
....| New Folder
......| New Files
........| New Folder

What it should do

Main Folder...
Rename folder.bat
....| New Folder
......| New Files
........| New Files

Need help with this

I even tried this

@echo off

for /f %%f in ('dir /s /b /a:d "New Folder"') do ren "%%f" "New Files"

CodePudding user response:

Firstly, you seem to confuse for /D with for /F, and it is the latter you need to use here:

for /F "tokens=*" %%I in ('dir /S /B /A:D-H-S "New Folder"') do (
    …
)

This iterates through the whole directory tree rooted at the current working directory (which is not necessarily the container of the batch script!). The tokens=* option of for /F ensures that white-spaces in the paths are maintained (removing leading ones, but such are anyway not possible for paths). The switch /A:D-H-S of dir ensures that files, as well as hidden and system directories, are excluded.

But there is one more problem: dir /S returns the items in a per-branch basis from top to the bottom of the hierarchy, and for /F, together with dir, lets the whole directory tree be built before loop iteration begins. So when now renaming a certain directory in the loop body, any children of it get new paths, although the old ones are buffered, letting the renaming fail. For instance, when directory D:\Path\To\Root\New Folder becomes renamed to D:\Path\To\Root\New Files, a sub-directory D:\Path\To\Root\New Folder\New Folder does no longer exist, since its path is now D:\Path\To\Root\New Files\New Folder, hence it cannot be found.

To overcome this issue, simply reverse the whole directory tree to be iterated by sort:

cd /D "D:\Path\To\Root" & rem // (use `%~dp0.` to specify a path relative to the container of this script)
for /F "tokens=*" %f in ('dir /S /B /A:D-H-S "New Folder" ^| sort /R') do (
    ren "%%f" "New Files"
)

That way, the sub-directory D:\Path\To\Root\New Folder\New Folder is renamed first, before its parent D:\Path\To\Root\New Folder becomes processed.

  •  Tags:  
  • Related