我想在调用函数中更改计数器变量。
在我的示例中,我想计算所有子文件夹(= count_all),然后我想增加它成功进行的子文件夹的计数(= 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
该代码有效,但我不明白setlocal enabledelayedexpansion
. 当我把它放在调用中:process_subfolders
时,我无法再更改变量,我不知道如何相应地返回它们。不知何故,我可以在调用结束时设置它们endlocal & set "%2=whatever"
(或类似的东西),但我仍然不明白哪些变量是本地的,哪些不是。
和什么时候应该使用 which 和count_all
有什么区别?%count_all%
!count_all!