Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

math

windows

cmd

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'?

like image 386
prosody Avatar asked Sep 15 '25 12:09

prosody


1 Answers

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.

like image 100
MohsenEMX Avatar answered Sep 17 '25 04:09

MohsenEMX