-2

I am new to batch scripting and need to figure out how to use values that were input by the user to create a variable.

@echo off
set /p userMonth = "Enter Month(mm): "
set /p userDay = "Enter Day (dd): "
set /p userYear = "Enter Year (yyyy): "

echo month: %userMonth%
echo day: %userDay%
echo year: %userYear%

set today= %userMonth%.%userDay%.%userYear%
set month= %userMonth%-%userYear%

When I run the script the output just says

month:
day:
year:

After I input (for example)

01
25
2018
  • 3
    Please [edit] your question title to something that describes the problem you're having or question you're asking. It should be clear enough to be of some value to a future reader here who is scanning a list of search results. The current title contains nothing at all that will be helpful in that regard. Thanks. – Ken White Jan 15 '19 at 01:00
  • 2
    Remove the spaces before and after the `=` on each line and see if that helps. Your variables are named `usermonth ` (note the blank space at the end), not `usermonth`. – Ken White Jan 15 '19 at 01:01
  • `set` command evaluates and includes every single character you type since the first word begin. So yes, your vairbales are valid, but they are not what you think. so `userMonth = "Enter Month(mm): "` will have a variable of `%userMonth %` (note the space before the last `%` . Also the last 2 will have values with a starting space. Always enclose your full variable with double quotes as well. i.e `set "month=%userMonth%-%userYear%"` which does not form part of the variable or value, it simply eliminates unwanted whitespace after. – Gerhard Jan 15 '19 at 06:27

1 Answers1

0

You have your set command formatted wrong. When using quotes, it goes " string= data ". Keep in mind you also don't want spaces between string & =. Use set /? to find more info.

set /p "userMonth=Enter Month(mm): "
set /p "userDay=Enter Day (dd): "
set /p "userYear=Enter Year (yyyy): "

echo month: %userMonth%
echo day: %userDay%
echo year: %userYear%

Furthermore, for subtracting or adding strings, you need to use the set /a command. The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated.

set "today=%userMonth%.%userDay%.%userYear%"
set /a "month=%userMonth%-%userYear%"

Further 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"
John Kens
  • 1,615
  • 2
  • 10
  • 28
  • subtracting the year from the month doesn't make sense. Concatenating the month with a dash and a year perfectly does (Month=01, Year=2018. `set /a ...`=-2017, `set `=01-2018) – Stephan Jan 15 '19 at 07:47
  • 1
    @Stephan I'm not here to question the OP's reasons. Just fixed his script with pointers on how to improve. – John Kens Jan 15 '19 at 10:12
  • if you feel, that `set month= %userMonth%-%userYear%` should be `set /a month= %userMonth%-%userYear%`, that's ok. I don't think so, but ymmv. – Stephan Jan 15 '19 at 15:46