Batch file 如果占用语句时间,则批处理文件

Batch file 如果占用语句时间,则批处理文件,batch-file,if-statement,environment-variables,set-operations,Batch File,If Statement,Environment Variables,Set Operations,我正在尝试编写一个批处理文件,该文件占用命令所用的时间,并将其转换为小时、分钟和秒 以下是我得到的: @echo off set /a sec=0 set /a min=0 set /a hrs=0 for /f %%G in ('ping localhost -n 100 >nul') do ( ping localhost -n 1 >nul set /a sec+=1 if %sec%==60 ( set /a min+=1

我正在尝试编写一个批处理文件,该文件占用命令所用的时间,并将其转换为小时、分钟和秒

以下是我得到的:

@echo off

set /a sec=0
set /a min=0
set /a hrs=0
for /f %%G in ('ping localhost -n 100 >nul') do (
    ping localhost -n 1 >nul
    set /a sec+=1
    if %sec%==60 ( 
        set /a min+=1
        set /a sec=0
    )
    if %min%==60 (
        set /a hrs+=1
        set /a min=0
    ) 
)
echo Time taken: %hrs%:%min%:%sec%.
我不断得到一个“)在这个时候是意外的。”错误。FOR循环确实有效,问题在于IF语句

我尝试过使用EQU运算符并添加引号,但没有效果。有人能帮忙吗


另外,我在某个地方读到,set运算符可能无法在IF语句下工作-这是真的吗?

您需要在批处理文件的顶部使用它

setlocal enabledelayedexpansion
还有
!秒
!敏语法,而不是使用
%
来更改和使用循环中的变量

如果没有延迟扩展,则不能在循环中使用set更改变量,也不能在同一个循环或嵌套的循环集中使用该变量