BATCH - 向函数添加计数器(用于/ loop)

时间:2013-11-29 20:45:58

标签: windows batch-file counter counting

我在BATCH文件中使用这些函数:

set /p time="Enter seconds  "

for /f %%C in ('Find /V /C "" ^< list.txt') do set Count=%%C

for /f "usebackq delims=" %%i in ("list.txt") do (
  start "" "firefox.exe" -new-tab "http://www.website.com/%%i"
  >nul ping -n %time% localhost

我搜索了一种向最后一个函数添加计数器* 的方法,所以它会在Commandbox中告诉我有关进度的信息,但我发现的只是一个“独立”函数,如下所示:

setlocal enableextensions enabledelayedexpansion
SET /A COUNT=1
FOR /F "tokens=*" %%A IN (config.properties) DO (
  SET /A COUNT+=1
  ECHO !COUNT!
)
endlocal

(可在此处找到:Counting in a FOR loop using Windows Batch script

你能告诉我如何将第二个功能(计数器)添加到第一个(firefox-tab-opening)吗?

* =我的意思是每当for-Function在Firefox中打开一个新的Tab时,批处理都会在cmd.exe-Box中放入一个计数(最好与某种覆盖而不是新行相同的行)每个计数)后面的总行数(%Count%)(如23/80,当新Tab打开24/80等等)

非常感谢你宝贵的时间帮助我:)

编辑:我想我会分享完整的非工作版

for /f %%C in ('Find /V /C "" ^< list.txt') do set TotalCount=%%C
ECHO (List with %TotalCount% Lines)

set /p time="Enter seconds  "



set /a counter=1

for /f "usebackq delims=" %%i in ("list.txt") do (
  start "" "firefox.exe" -new-tab "http://www.website.com/%%i" 

  set /a count+=1
  echo %counter% / %TotalCount%

  >nul ping -n %time% localhost


)

问题:它一直发出“1 / %TotalCount%”! (%TotalCount%工作正常)

2 个答案:

答案 0 :(得分:1)

enabledelayedexpansion需要在循环中回显变量,cls是使用静态数字更新屏幕的常用简单方法。

@echo off
setlocal enabledelayedexpansion
for /f %%C in ('Find /V /C "" ^< list.txt') do set TotalCount=%%C
ECHO (List with %TotalCount% Lines)

set /p time="Enter seconds  "
set /a counter=0

for /f "usebackq delims=" %%i in ("list.txt") do (
  start "" "firefox.exe" -new-tab "http://www.website.com/%%i" 
  cls
  set /a counter+=1
  echo !counter! / !TotalCount!
  >nul ping -n %time% localhost
)

答案 1 :(得分:-1)

  

我如何制作&#34; new&#34; !计数器!覆盖旧!柜台! (所以   保持在同一行,而不是每次都换行?

@echo off
setlocal EnableDelayedExpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
for /l %%n in (1,1,10000) do (
    set /P "=Count %%n /10000!CR!" <nul
)

基于(好的,基本上是从)来自这里的用户jeb:Echoing in the same line