I am getting wrong output even after files were properly deleted in all subdirectories in both directories. Please make those changes so it looks like i expected.

[SCRIPT]

@echo off
setlocal EnableExtensions EnableDelayedExpansion

set "directories=C:\Users\PESZQ\Desktop\test\tutaj C:\Users\PESZQ\Desktop\test\tutaj2"

:: Initialize the summary count
set "SUMMARY_COUNT=0"
set "TOTAL_FILES_DELETED=0"

for %%D in (%directories%) do (
    echo Processing directory %%D ...
    set "ROOT_DIR=%%D"

    :: Get the root directory name
    for %%R in ("!ROOT_DIR!") do set "ROOT_NAME=%%~nxR"

    :: Get all directories including subdirectories
    for /f "delims=" %%G in ('dir /AD /S /B "!ROOT_DIR!" 2^>nul') do (
        set "DIR_TO_PROCESS=%%G"

        :: Initialize FILECOUNT for current directory
        set "FILECOUNT=0"

        :: Count files older than 15 days in DIR_TO_PROCESS
        for /f "delims=" %%H in ('forfiles /p "!DIR_TO_PROCESS!" /m * /d -15 2^>nul ^| find /c /v ""') do (
            set "FILECOUNT=%%H"
        )

        if !FILECOUNT! gtr 0 (
            echo !FILECOUNT! files in directory "!DIR_TO_PROCESS!" are older than 15 days. Deleting them...
            :: Delete the files
            forfiles /p "!DIR_TO_PROCESS!" /m * /d -15 /c "cmd /c del @path" >nul 2>nul
            echo Deleted !FILECOUNT! files in directory "!DIR_TO_PROCESS!".

            :: Get the relative path from the root directory
            set "RELATIVE_DIR=!DIR_TO_PROCESS:%ROOT_DIR%\=!"

            :: Handle the case where the directory is the root
            if "!RELATIVE_DIR!"=="" set "RELATIVE_DIR=."

            :: Increment summary count and store the line
            set /a SUMMARY_COUNT+=1
            set /a TOTAL_FILES_DELETED+=!FILECOUNT!
            set "SUMMARY_LINE[!SUMMARY_COUNT!]=Files deleted inside !RELATIVE_DIR!: !FILECOUNT!"
        )
    )
)

:: Display the summary
echo.
echo Summary of files deleted:
if !SUMMARY_COUNT! gtr 0 (
    for /L %%i in (1,1,!SUMMARY_COUNT!) do (
        call echo !SUMMARY_LINE[%%i]!
    )
    echo Total files deleted: !TOTAL_FILES_DELETED!
) else (
    echo No files were deleted.
)

pause

[WRONG OUTPUT]

Processing directory C:\Users\PESZQ\Desktop\test\tutaj ...
8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj\1" are older than 15 days. Deleting them...
Deleted 8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj\1".
8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj\2" are older than 15 days. Deleting them...
Deleted 8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj\2".
8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj\3" are older than 15 days. Deleting them...
Deleted 8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj\3".
Processing directory C:\Users\PESZQ\Desktop\test\tutaj2 ...
8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj2\1" are older than 15 days. Deleting them...
Deleted 8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj2\1".
8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj2\2" are older than 15 days. Deleting them...
Deleted 8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj2\2".
8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj2\3" are older than 15 days. Deleting them...
Deleted 8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj2\3".

Summary of files deleted:
Files deleted inside C:UsersPESZQDesktoptesttutaj1: 8
Files deleted inside C:UsersPESZQDesktoptesttutaj2: 8
Files deleted inside C:UsersPESZQDesktoptesttutaj3: 8
Files deleted inside C:UsersPESZQDesktoptesttutaj21: 8
Files deleted inside C:UsersPESZQDesktoptesttutaj22: 8
Files deleted inside C:UsersPESZQDesktoptesttutaj23: 8
Total files deleted: 48
Press any key to continue . . .


[EXPECTED OUTPUT]

Processing directory C:\Users\PESZQ\Desktop\test\tutaj ...

8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj\1" are older than 15 days. Deleting them...
Deleted 8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj\1".
8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj\2" are older than 15 days. Deleting them...
Deleted 8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj\2".
8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj\3" are older than 15 days. Deleting them...
Deleted 8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj\3".


Processing directory C:\Users\PESZQ\Desktop\test\tutaj2 ...

8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj2\1" are older than 15 days. Deleting them...
Deleted 8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj2\1".
8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj2\2" are older than 15 days. Deleting them...
Deleted 8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj2\2".
8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj2\3" are older than 15 days. Deleting them...
Deleted 8 files in directory "C:\Users\PESZQ\Desktop\test\tutaj2\3".


Summary of files deleted:

Files deleted inside 1: 8
Files deleted inside 2: 8
Files deleted inside 3: 8
Files deleted inside 1: 8
Files deleted inside 2: 8
Files deleted inside 3: 8

Total files deleted: 48

Press any key to continue . . .

[ADDITIONAL COMMENT]

Pay attention to the number of empty rows for better visibility, fix Files deleted inside C:UsersPESZQDesktoptesttutaj23: 8 showing whole path instead of just the subdir name.