TCN
TCN

Reputation: 1651

Missing operand error in batch statement with variable

I'm getting a syntax error on the timeout command supposedly because of a "missing operand". But I don't see any operand missing.

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

Upvotes: 2

Views: 2850

Answers (2)

JosefZ
JosefZ

Reputation: 30143

Invalid syntax. Commenting commands inline is not possible in cmd CLI nor a batch scripts:

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

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

Note that %timeTo%*60 exceeds timeout /T valid range -1 to 99999 for default 3600*60.

If your code snippet appears enclosed in () parentheses, then use 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  
)

Output: note that set /a timeTo=%timeTo%*60 line causes the Missing operand error as results to erroneous set /a timeTo=*60 in the parse time.

==>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

==>

Upvotes: 2

npocmaka
npocmaka

Reputation: 57272

Batch does not support inline comments.All comments need to be in separate line:

::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  

Upvotes: 1

Related Questions