0

I'm trying to build a batch file which will carry out a rename of all subdirectories in a location in the format of 'parent directory filename' followed by underscore followed by '4 digit incrementing number' (new number count for each directory). So for example ABCDEF_0001, ABCDEF_0002, etc. I currently do this using windows software but would love to have the ability to run it as a batch file.

Many thanks!

EDIT - If possible using an address in the bat file rather than drag and drop.

1 Answer 1

1

Please se if this is like expected:

enter image description here

You have to drag and drop the parent folder to the batch file...

@echo off
if exist "%~1" (if not exist "%~1\" exit) else (exit)

set Parent=%~nx1
pushd "%~1"

for /f "delims=" %%a in ('dir /b /ad') do call :Edit "%%a"
exit

:Edit
set /a Counter+=1
set CounterZ=000%Counter%
set CounterZ=%CounterZ:~-4%
ren "%~1" "%Parent%_%CounterZ%"
goto :EOF
1
  • This script could fail if you add a subdirectory and run the script a second time.
    – Niccolo M.
    Commented Dec 7, 2022 at 17:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .