在带有变量的批处理语句中缺少操作数错误

时间:2015-09-05 07:51:00

标签: batch-file cmd

由于“缺少操作数”,我在超时命令上遇到语法错误。但我没有看到任何操作数丢失。

ng-change

2 个答案:

答案 0 :(得分:2)

语法无效。在cmd CLI或批处理脚本中无法对内联注释命令:

==>set /a timeto=3600            ::setting default time (1 hour)
Missing operator.

==>set /a timeto=3600
3600
==>

请注意,对于默认%timeTo%*60timeout /T超出-1有效范围999993600*60

如果您的代码段显示在()括号中,请使用Delayed Expansion

@ECHO ON >NUL
@SETLOCAL EnableExtensions EnableDelayedExpansion
if 1==1 (
  rem description for the user to read
  echo Insert time in minutes:  
  rem setting default time (1 hour = 60 minues)
  set /a timeTo=60            
  rem asking user for input (integer)
  set /p timeTo=                
  rem converting minutes to seconds - erroneous
  set /a timeTo=%timeTo%*60     
  rem converting minutes to seconds - right approach
  set /a timeTo=!timeTo!*60     
  rem command based on the inputted value
  echo timeout /t !timeTo! /nobreak  
)

输出:请注意,set /a timeTo=%timeTo%*60行会导致Missing operand错误,导致解析时错误为set /a timeTo=*60

==>D:\bat\SO\32410773.bat

==>if 1 == 1 (
rem description for the user to read
 echo Insert time in minutes:
 rem setting default time (1 hour = 60 minues)
 set /a timeTo=60
 rem asking user for input (integer)
 set /p timeTo=
 rem converting minutes to seconds - erroneous
 set /a timeTo=*60
 rem converting minutes to seconds - right approach
 set /a timeTo=!timeTo!*60
 rem command based on the inputted value
 echo timeout /t !timeTo! /nobreak
)
Insert time in minutes:

Missing operand.
timeout /t 3600 /nobreak

==>

答案 1 :(得分:1)

批处理不支持内联注释。所有注释都需要在单独的行中:

::description for the user to read 
echo Insert time in minutes:
::setting default time (1 hour)  
set /a timeto=3600
::asking user for input (integer)            
set /p timeto=
::converting minutes to seconds                
set /a timeto=%timeto%*60
::command based on the inputted value    
timeout /t %timeto% /nobreak