计数 For 循环,每行开头都有数字
Count For loop with number at start of each line
我一直在想如何计算循环次数并在循环开始时显示这个数字。
:Search
set location=%cd%
cls
echo.
Echo. Searching for .exe files in %location% and subfolders
echo.
set /a count=0
echo.___________________________________________
echo.Found:
echo.
FOR /r %%i in (*.exe) do (echo. %count%. %%~ni & set /a count+=1)
echo.___________________________________________
echo.
title Exe blocker %count% Files found
echo. Number of files found with .exe extention: %count%
echo.
echo.
以上代码在文件夹(和子文件夹)中搜索 exe
文件。我希望代码显示:
1. Firstexefilename
2. Secondexefilename
但是显示
0. FirstexefileName
0. SecondexefileName
最终的计数变量可以正常显示 2。
下一个代码片段显示了延迟扩展的一些解决方案:enabled (expand variables at execution time rather than at parse time) or disabled (expand variables at parse time rather than at execution time, this is the default behaviour):
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
:Search
set "location=%cd%"
cls
echo(
echo( Searching for .exe files in %location% and subfolders
echo(
set /a count=0
echo(___________________________________________
echo(Found:
echo(
FOR /r %%i in (*.exe) do (echo( !count!. %%~ni & set /a count+=1)
echo(___________________________________________
echo(
title Exe blocker %count% Files found
echo( Number of files found with .exe extention: %count%
echo(
echo(
ENDLOCAL
echo( Another approach using `findstr.exe` and delayed expansion disabled:
echo(
SETLOCAL EnableExtensions DisableDelayedExpansion
FOR /F "tokens=1,* delims=:" %%i in ('
dir /B /S *.exe^|findstr /N "^"
') do (echo( %%i. %%~nj & set /a "counter=%%i")
echo(___________________________________________
echo(
echo( Number of files found with .exe extention: %counter%
ENDLOCAL
与您的原始解决方案相对应的另一种方法:
SETLOCAL EnableExtensions
:Search
set "location=%cd%"
rem cls
echo(
echo( Searching for .exe files in %location% and subfolders
echo(
set /a count=0
echo(___________________________________________
echo(Found:
echo(
FOR /r %%i in (*.exe) do (
set "_filename=%%~ni"
call :usevariable
set /a "count+=1"
)
echo(___________________________________________
echo(
title Exe blocker %count% Files found
echo( Number of files found with .exe extention: %count%
echo(
echo(
ENDLOCAL
goto :eof
:usevariable
echo( %count%. %_filename%
goto :eof
我一直在想如何计算循环次数并在循环开始时显示这个数字。
:Search
set location=%cd%
cls
echo.
Echo. Searching for .exe files in %location% and subfolders
echo.
set /a count=0
echo.___________________________________________
echo.Found:
echo.
FOR /r %%i in (*.exe) do (echo. %count%. %%~ni & set /a count+=1)
echo.___________________________________________
echo.
title Exe blocker %count% Files found
echo. Number of files found with .exe extention: %count%
echo.
echo.
以上代码在文件夹(和子文件夹)中搜索 exe
文件。我希望代码显示:
1. Firstexefilename
2. Secondexefilename
但是显示
0. FirstexefileName
0. SecondexefileName
最终的计数变量可以正常显示 2。
下一个代码片段显示了延迟扩展的一些解决方案:enabled (expand variables at execution time rather than at parse time) or disabled (expand variables at parse time rather than at execution time, this is the default behaviour):
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
:Search
set "location=%cd%"
cls
echo(
echo( Searching for .exe files in %location% and subfolders
echo(
set /a count=0
echo(___________________________________________
echo(Found:
echo(
FOR /r %%i in (*.exe) do (echo( !count!. %%~ni & set /a count+=1)
echo(___________________________________________
echo(
title Exe blocker %count% Files found
echo( Number of files found with .exe extention: %count%
echo(
echo(
ENDLOCAL
echo( Another approach using `findstr.exe` and delayed expansion disabled:
echo(
SETLOCAL EnableExtensions DisableDelayedExpansion
FOR /F "tokens=1,* delims=:" %%i in ('
dir /B /S *.exe^|findstr /N "^"
') do (echo( %%i. %%~nj & set /a "counter=%%i")
echo(___________________________________________
echo(
echo( Number of files found with .exe extention: %counter%
ENDLOCAL
与您的原始解决方案相对应的另一种方法:
SETLOCAL EnableExtensions
:Search
set "location=%cd%"
rem cls
echo(
echo( Searching for .exe files in %location% and subfolders
echo(
set /a count=0
echo(___________________________________________
echo(Found:
echo(
FOR /r %%i in (*.exe) do (
set "_filename=%%~ni"
call :usevariable
set /a "count+=1"
)
echo(___________________________________________
echo(
title Exe blocker %count% Files found
echo( Number of files found with .exe extention: %count%
echo(
echo(
ENDLOCAL
goto :eof
:usevariable
echo( %count%. %_filename%
goto :eof