1
0 Comments

I aim to modify a counter variable within a function call. Specifically, I need to keep track of the total number of subfolders (represented by “count_all”), and then increase the count of subfolders that the function successfully processed (indicated by “count_done”).

    @echo off
    cls

    setlocal enableextensions enabledelayedexpansion
    set /a "count_all=0"
    set /a "count_done=0"

    for /d /r %%i in (.\*) do call :process_subfolders "%%i"

    if %count_all% EQU 0 (
        echo No archives found.
    ) else (
        set /a "counted=1"
        for /d /r %%i in (.\*) do call :process_subfolders "%%i"
    )

    pause
    endlocal
    goto :eof

    ::___________________________________________________________________
    :process_subfolders
    set "folder=%~nx1"

    pushd %folder%
    if exist *.rar (
        if !counted! equ 0 (
            set /a "count_all+=1"
        ) else (
            set /a "count_done+=1"
            echo !count_done!: %folder%

            rem ... do something with the rar files in this folder ...
            rem ... for testing: use ping
            ping 127.0.0.1 -n 2 > nul

            set /a "percent=!count_done!*100/!count_all!"
            title !percent!%% [!count_done!/!count_all!]
        )
    )
    popd
    exit /b

The code is functional, but I am unsure about the purpose and effects of the “setlocal enabledelayedexpansion” command. When I apply it to the “call :process_subfolders” function, I am unable to modify the variables and I am unsure how to properly return them. I found a potential solution by using “endlocal & set%2=whatever“, but I am still confused about which variables are local and which ones are not.

Additionally, I am uncertain about the distinction between “count_all“, “%count_all%“, and “!count_all!“, and when each should be used?

Askify Moderator Edited question April 30, 2023