Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying Two Whole Numbers In Batch

I am trying to make a Shutdown dialogue in Batch and I have run into a slight problem. I don't like how Windows 8 asks you for the time in seconds when you would like to remote shutdown your own computer with a timer and I am trying to make a batch file that converts a given number (minutes) into seconds.

I have searched the vast majority of the interwebs and cannot find a way to multiply two whole numbers in a batch file.

Here is what I have so far:

@echo off  
echo Enter a number:  
set /p %num1%=  
echo Enter another:  
set /p %num2%=  
set /a sum1=%num1%*%num2%  
echo The total is %sum1%  
pause  

Could some kind soul please tell me where I have gone wrong?

Thanks Charlie B

like image 243
ChaBat10 Avatar asked Dec 08 '13 10:12

ChaBat10


3 Answers

fix to

@echo off  
echo Enter a number:
set /p num1=  
echo Enter another:
set /p num2=
set /a sum1="num1 * num2"
echo The total is %sum1%  
pause 
like image 102
BLUEPIXY Avatar answered Oct 20 '22 16:10

BLUEPIXY


This will do what you want:

@echo off
Echo Time to Shutdown:
set /p "min=Time(Min): "
set /a sec=min*60
shutdown /t %sec%

That doesn't handle invalid input, but for your program that won't be a problem. (If you want it to error handle comment so).

Mona

like image 30
Monacraft Avatar answered Oct 20 '22 16:10

Monacraft


You don't have to put the "%" in the declaration of your variables

@echo off  
echo Enter a number:  
set /p num1=  
echo Enter another:  
set /p num2=  
set /a sum1=%num1%*%num2%  
echo The total is %sum1%  
pause  
like image 2
SachaDee Avatar answered Oct 20 '22 15:10

SachaDee