Reputation: 83
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
Upvotes: 7
Views: 24290
Reputation: 1
I recently wrote a batch version of expr:
@echo off
setlocal
if %1.==. goto help
echo.
echo expr: integer math only!
echo.
(set /a ans=%*)
echo ans: %ans%
exit /b
:help
echo expr: command line calculator. v0.01 (2024-Nov-03)
echo usage: expr {your expression here}
As my code indicates I wasn't able to get my little script to perform the math using real (float) numbers. However, it's good enough to work as a four function integer calculator. When I researched this further, I discovered the only way to calculate floating values in windows was to use powershell or a windows version of bash. I'm actually thinking of recreating expr in C that will properly parse the command line, but this is way down on the priorities list.
Upvotes: 0
Reputation: 40145
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
Upvotes: 8
Reputation: 6620
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
Upvotes: 6
Reputation: 9545
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
Upvotes: 2