批处理文件中的计数器进行循环-如何将数字保持在同一位置? [英] Counter in Batch file for loop - how to keep the number in the same place?
问题描述
我正在Windows中编写一个批处理文件.我需要逐行处理一个大文本文件.我想在执行此操作的同时在cmd
窗口中显示一个计数器.以下代码效果很好:
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!
)
唯一的问题是,每增加一个计数,我都会得到一个新行.所以cmd
窗口中的输出是这样的:
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
我该怎么做?
帖子 Windows批处理:没有换行的回声确实说明了如何进行回显,而无需在终端中开始新行.所以这就是我尝试实现的方式:
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
...
我真正想要的是:
---------------------
- START PROCESS -
---------------------
count = 124 <= this number should dynamically increment
推荐答案
以下代码似乎有效:
@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
)
这篇关于批处理文件中的计数器进行循环-如何将数字保持在同一位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!