'how/can i use math inside a windows cmd command?

i regularly set my pc to shutdown on a timer using "shutdown.exe /t [time in seconds] /s"

which leads to me doing late night mental arithmetic...

can i somehow embed math into the shutdown.exe call, eg some variation of "shutdown.exe /t 120*60 /s"?

i know i can do "set /a 120*60" first to output the number and then use it in the shutdown call, but then i could also work it out in my head fairly quickly - i'm interested in the more general question of using math in cmd calls...

thanks!

p.s i did try to search google and SO for answers but the search terms i can think of are too general - "use math in cmd command" yields answers about how to use cmd to solve math problems rather than to use math in the middle of a, umm, i don't even know the term - 'function call'?



Solution 1:[1]

A script I made for you, copy it and place it in some .bat or .cmd file.

@echo off
title Shutdown PC
:start
set /p var1=Select number one:
set /p var2=Select number two:
echo So, This is number one: %var1% & echo And this is number two: %var2%
set /p check=Are selected numbers okay? ( 1 or 0 ) :
if (%check%==1) (
set /a var3=%var1%*%var2%
shutdown.exe /t %var3% /s
pause
) else (
cls
GOTO start
)
pause

It asks for 2 numbers, then asks to check if your selected numbers are okay or not, and then checks for your answer, if answer is "1" it will power number one into number two and the result will be time for windows to shutdown in seconds. if answer is "0" it will run whole process again.

Solution 2:[2]

First, you need to define a variable with a command line like this:

set var1=29

And another one:

set var2=31

And use them:

set /A var3=var1+var2
echo %var3%

You can also get variables by user:

set /P "var4=Enter a number: "

and use it like this:

echo %var3%+%var4%

Don't forget to use pause on running the batch file with a double click.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MohsenEMX
Solution 2 Mofi