循环批处理文件中的计数器 - 如何将数字保存在同一个地方?

时间:2016-06-21 07:50:07

标签: windows batch-file cmd terminal

我正在Windows中编写批处理文件。我需要逐行处理一个大文本文件。这样做时,我想在cmd窗口显示一个计数器。以下代码非常有用:

@echo off
setlocal enableextensions enabledelayedexpansion
set /a count=1

for /f "tokens=*" %%A in (myFile.txt) do (
    set /a count+=1
    echo.!count!
)

唯一的问题是我为每个计数增量得到新行。所以cmd窗口中的输出是这样的:

---------------------
-   START PROCESS   -
---------------------
1
2
3
4
..
1000

我真正想要的是在终端窗口中显示动态数字。像这样:

---------------------
-   START PROCESS   -
---------------------
count = 124           <= this number should dynamically increment

我该怎么做?

编辑:

帖子Windows batch: echo without new line确实解释了如何在终端中没有开始新线路的情况下进行回声。所以这就是我尝试实现它的方式:

@echo off
setlocal enableextensions enabledelayedexpansion
set /a count=1

for /f "tokens=*" %%A in (iconOldPathList.txt) do (
    set /a count+=1
    <nul set /p =!count!
)

不幸的是,结果不是动态更新的数字。我得到的是:

---------------------
-   START PROCESS   -
---------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
...

我真正想要的是:

---------------------
-   START PROCESS   -
---------------------
count = 124           <= this number should dynamically increment

1 个答案:

答案 0 :(得分:1)

以下代码似乎有效:

@echo off
setlocal EnableDelayedExpansion

for /f %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"

for /L %%n in (100 -1 1) do (
    <nul set /P "=This window will close in %%n seconds   !CR!"
    ping -n 2 localhost > nul
)