How to Subtract Values from a Numeric String, in Windows Command Line?

How to Subtract Values from a Numeric String, in Windows Command Line?

In Windows command-line I can print the year of the current date by doing this:

echo %date:~10,4%

result: 2016

Of course, depending on how dates are displayed in your language you need to change the 10 or 4 to get the correct sub string. But that's ok.

What I want it to print is 2015, which is 2016 - 1. So, how do I subtract 1 from the sub string I've just captured? There is no problem in using auxiliary variables.

1

2 Answers

As you've stated there's no problem using another variable, you can do it as follows (My environment settings use a different substring)

set /a "yearminus1=%date:~6,10%-1"

For you, I assume this will be set /a "yearminus1=%date:~10,4%-1", but I can't test this.

Using the /a switch with set specifies you will be giving an expression to be evaluated, rather than just store the expression in the variable as it is.

The following operators can be used:

   +   Add                set /a "_num=_num+5"
   +=  Add variable       set /a "_num+=5"
   -   Subtract (or unary)set /a "_num=_num-5"
   -=  Subtract variable  set /a "_num-=5"
   *   Multiply           set /a "_num=_num*5"
   *=  Multiply variable  set /a "_num*=5"
   /   Divide             set /a "_num=_num/5"
   /=  Divide variable    set /a "_num/=5"
   %%  Modulus            set /a "_num=5%%2"
   %%= Modulus            set /a "_num%%=5" 
   !   Logical negation  0 (FALSE) ⇨ 1 (TRUE) and any non-zero value (TRUE) ⇨ 0 (FALSE)
   ~   One's complement (bitwise negation) 
   &   AND                set /a "_num=5&3"    0101 AND 0011 = 0001 (decimal 1)
   &=  AND variable       set /a "_num&=3"
   |   OR                 set /a "_num=5|3"    0101 OR 0011 = 0111 (decimal 7)
   |=  OR variable        set /a "_num|=3"
   ^   XOR                set /a "_num=5^3"    0101 XOR 0011 = 0110 (decimal 6)
   ^=  XOR variable       set /a "_num=^3"
   <<  Left Shift.    (sign bit ⇨ 0)
   >>  Right Shift.   (Fills in the sign bit such that a negative number always remains negative.)
                       Neither ShiftRight nor ShiftLeft will detect overflow.
   <<= Left Shift variable     set /a "_num<<=2"
   >>= Right Shift variable    set /a "_num>>=2"

  ( )  Parenthesis group expressions  set /a "_num=(2+3)*5"
   ,   Commas separate expressions    set /a "_num=2,_result=_num*5"

From here.

2

How do I set a variable that is the year of last year?

Use the following batch file (test.cmd):

@echo off
setlocal
setlocal enabledelayedexpansion
for /f "usebackq delims=/ tokens=3 " %%i in (`date /t`) do (
  echo Current year is %%i
  set /a "_lastyear=%%i-1"
  echo Last year is !_lastyear!
  )
endlocal

Output:

F:\test>test
Current year is 2016
Last year is 2015

In your particular case you should be able to use the following:

set_year=%date:~10,4%
set /a "_lastyear=%%i-1"
echo %_lastyear%

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • date - Display or change the date.Display or change the date.
  • for /f - Loop command against the results of another command.
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.