不平衡的括号和缺失的算子

时间:2015-05-27 07:53:12

标签: windows batch-file

以下是一个批处理文件,它调用for循环中的另一个批处理文件,它循环当前文件夹的所有txt文件。

@
echo off
setlocal
for % % i in ( * .txt) do(
    callSingle.bat -if = % % i - of = % ~dp0
)
endlocal@ 
echo on

单个bat文件如下 它为每次执行写入日志文件。 在命令提示符下执行后显示

Unbalanced Parenthesis.
Unbalanced Parenthesis.
Missing Operator.

打开日志文件时显示开始时间和结束时间是否正确。但持续时间错误

@echo off
setlocal

echo ************************************************************************** > %2.log
echo *                     Log File for %2                             * >> %2.log
echo ************************************************************************** >> %2.log
rem The format of %TIME% is HH:MM:SS,CS for example 23:59:59,99
set STARTTIME=%TIME%

rem here begins the command you want to measure
some_executable.exe -if=%2 -of=%4
rem here ends the command you want to measure

set ENDTIME=%TIME%

rem output as time
echo STARTTIME: %STARTTIME% >> %2.log
echo ENDTIME: %ENDTIME% >> %2.log

rem calculating the duratyion is easy
rem set /A DURATION=%ENDTIME%-%STARTTIME%

rem convert STARTTIME and ENDTIME to milliseconds
set /A STARTTIME=(10%STARTTIME:~0,2%-1000)*3600000 + (10%STARTTIME:~3,2%-1000)*60000 + (10%STARTTIME:~6,2%-1000)*1000 + (10%STARTTIME:~9,2%-1000)
set /A ENDTIME=(10%ENDTIME:~0,2%-1000)*3600000 + (10%ENDTIME:~3,2%-1000)*60000 + (10%ENDTIME:~6,2%-1000)*1000 + (10%ENDTIME:~9,2%-1000)

rem calculating the duratyion is easy
set /A DURATION=%ENDTIME%-%STARTTIME%

rem we might have measured the time inbetween days
if %ENDTIME% LSS %STARTTIME% set /A DURATION=%STARTTIME%-%ENDTIME%

rem now break the milliseconds down to hors, minutes, seconds and the remaining milliseconds
set /A DURATIONH=%DURATION% / 3600000
set /A DURATIONM=(%DURATION% - %DURATIONH%*3600000) / 60000
set /A DURATIONS=(%DURATION% - %DURATIONH%*3600000 - %DURATIONM%*60000) / 1000
set /A DURATIONHS=(%DURATION% - %DURATIONH%*3600000 - %DURATIONM%*60000 - %DURATIONS%*1000)

rem some formatting
if %DURATIONH% LSS 10 set DURATIONH=0%DURATIONH%
if %DURATIONM% LSS 10 set DURATIONM=0%DURATIONM%
if %DURATIONS% LSS 10 set DURATIONS=0%DURATIONS%
if %DURATIONHS% LSS 10 set DURATIONHS=0%DURATIONHS%

echo DURATION: %DURATIONH%:%DURATIONM%:%DURATIONS%.%DURATIONHS% >> %2.log


endlocal
goto :EOF

这个问题有时会发生,否则就可以了。

1 个答案:

答案 0 :(得分:0)

一般调试建议echo ON,查看错误出现的位置和原因。添加一些pause ...

您的第一批文件代码完全搞砸了,请更正。

%TIME%的格式为HH:MM:SS,CS,例如23:59:59,99 ,但echo "%TIME%"已发出 ante meridiem 返回(例如)" 1:09:24,46"注意前导空格。因此,使用

set "STARTTIME=%TIME: =0%"

set "ENDTIME=%TIME: =0%"

请参阅this script repository on date and time arithmetic