2

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

Ken White
  • 123,280
  • 14
  • 225
  • 444
prosody
  • 557
  • 1
  • 3
  • 11
  • The only way to use math is with `set /a`. You can assign that result to a variable and use that variable in the command (as you've already described); that's your only option. – Ken White Mar 05 '22 at 02:44
  • See [this answer](https://stackoverflow.com/a/45330655/62576) for an example of setting a variable to the result of a math calculation. – Ken White Mar 05 '22 at 03:08
  • Please open a [command prompt](https://www.howtogeek.com/235101/), run `set /?` and read the output usage help carefully from top of first to bottom of last page explaining also the usage of an __arithmetic expression__ evaluated by `cmd.exe` during processing of the batch file. See also the Microsoft documentation for the [Windows commands](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) and [SS64.com - A-Z index of Windows CMD commands](https://ss64.com/nt/) if you need more information about the Windows commands in future. – Mofi Mar 05 '22 at 09:54

2 Answers2

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143
MohsenEMX
  • 31
  • 3
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.

MohsenEMX
  • 31
  • 3