问题
I'm writing a batch file in Windows. I need to process a big text file line by line. I'd like to show a counter in the cmd window while doing this. The following code works pretty good:
@echo off
setlocal enableextensions enabledelayedexpansion
set /a count=1
for /f "tokens=*" %%A in (myFile.txt) do (
set /a count+=1
echo.!count!
)
The only problem is that I get a new line for every count increment. So the output in the cmd window is something like this:
---------------------
- START PROCESS -
---------------------
1
2
3
4
..
1000
What I really want is a dynamic number displayed in the terminal window. Something like this:
---------------------
- START PROCESS -
---------------------
count = 124 <= this number should dynamically increment
How do I do that?
EDIT :
The post Windows batch: echo without new line does explain how to make an echo without starting a new line in the terminal. So this is how I tried to implement it:
@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!
)
Unfortunately, the result is not a dynamically updating number. What I get is:
---------------------
- 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
...
What I really want is:
---------------------
- START PROCESS -
---------------------
count = 124 <= this number should dynamically increment
回答1:
The following code seems to work:
@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
)
来源:https://stackoverflow.com/questions/37938489/counter-in-batch-file-for-loop-how-to-keep-the-number-in-the-same-place