Try to rename files in subfolder with subfolder‘s name

I‘m using Opus13.
I want to rename multiple files in multiple subfolders within a directory, using the name of each subfolder. The renaming should add a sequential number starting from 1 in order of the original file names, following the name of the subfolder.
Actually, I have a CMD script that meets this requirement, but I can't convert it to a button that can be used directly within opus, can anyone help?

@echo off
setlocal enabledelayedexpansion

for /d %%A in (*) do (
set "folder_name=%%~nxA"
set "index=1"

for %%B in ("%%A\*") do (
    set "file_name=%%~nxB"
    set "extension=%%~xB"
    set "new_name=!folder_name!_!index!!extension!"
    ren "%%B" "!new_name!"
    set /a index+=1
)
)

echo Renaming of files completed.
exit /b

The rename tool can do this, no need for a script.

Rename PATTERN=* TO={parent}-[#]-* IGNOREEXT NUMBER=1 RECURSE
1 Like

Subtle detail:

Note that the rename should be run on the folders, not the files themselves.

Usually it doesn’t matter but if you’re adding a counter it makes a difference. One way gives a count starting from 1 for each folder. The other would give a single count for all of the selected files, where the number would keep increasing instead of resetting to 1 each time the folder changes.

1 Like