batch-file - 批处理进度计数器

标签 batch-file cmd

我正在开发进度计数器,但是代码有什么问题吗?这始终显示 0% 并关闭应用程序。

@echo off
set /a i=0
set /a limit=100
set /a percent=0
echo %percent% percent
:execute
set /a i+=1
ping 127.0.0.1 -n 1 > nul
set /a percent = %i%/%limit%
cls
echo %percent% percent
if %i% LSS %limit% (goto execute) else exit

最佳答案

警告:变量定义的一个常见问题是变量名称中包含空格:

set /a percent = %i%/%limit%
              ^
              This space "can" be included in the name of the variable

虽然set/a 使用自己的解析器并会丢弃指定的空格,但其余的set 命令替代方案将创建名称中带有空格的变量。删除空格“更好/更安全/推荐”

问题:批量算术如何工作。所有算术运算符仅返回整数值。

set /a percent= %i% / %limit%
                    ^
                    Batch arithmetic handle only integers
                    i/limit will be always 0 for any i < limit

更改为

set /a percent= i * 100 / limit 

在这种情况下,不需要使用(但当然可以使用)%i% 来获取变量值。如前所述,set/a 使用自己的解析器,它将解析变量引用并检索精确值。

关于batch-file - 批处理进度计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27443922/

相关文章:

batch-file - 批处理 : Escaping with caret

cmd - 将文件复制到同一目录中的多个文件夹

windows - 删除以相同起始字符开头的文件夹

windows - 对于长路径和长文件名,删除超过 30 天的文件

batch-file - 批处理日期时间戳比较

windows - 从嵌套的批处理文件中退出

java - 无法从命令行运行命令 : ffmpeg doesn't convert file

windows - 如何使用批处理脚本将一些文件从一个目录移动到另一个目录?

windows - Ctrl+C 不能可靠地终止 Windows 批处理脚本

windows - 如何忽略 url 中的符号以替换批处理脚本中的字符串?