0

I tried to compare current date on being equal to a specific date using this code:

@echo on
if %date%=="Sun 10/15/2017" ( echo "Today is Sunday" ) else ( echo "Today is Unknown" )
pause

But on execution of the batch file I get just output the error message:

10/15/2017=="Sun 10/15/2017" was unexpected at this time.

What is the reason for this error?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Nichie
  • 1
  • 3
  • 2
    Nichie, please update [your last question](https://stackoverflow.com/q/46744276/6738015) with the solution you found, and also post the information requested in the [previous question to that](https://stackoverflow.com/a/46743856/6738015) too! Members are often reluctant to provide responses to those who fail to follow reasonable requests etc. In response to this question you haven't matched the doublequotes on either side of the comparison and we have no idea of the format of your `%date%` variable! – Compo Oct 14 '17 at 20:04
  • 1
    You have quotes on one side of the comparison to protect spaces. Why do you think you wouldn't need them on the other side? – Squashman Oct 14 '17 at 20:17

1 Answers1

1

The date environment variable reference %date% is replaced by Windows command interpreter during the preprocessing state by the region (country) dependent string before the IF command is executed at all.

On running the batch file from within a command prompt window it can be seen that the IF command line is after preprocessing:

if Sun 10/15/2017=="Sun 10/15/2017" ( echo "Today is Sunday" ) else ( echo "Today is Unknown" )

So the syntax of IF command is not correct after environment variable expansion because there is Sun as first argument, a space and Sun 10/15/2017=="Sun 10/15/2017" being interpreted as unexpected operator.

Whenever comparing strings it is recommended to use double quotes as only within a double quoted string the space character is not interpreted as argument separator. And the characters &()[]{}^=;!'+,`~|<> are also interpreted as literal characters within a double quoted string.

The double quotes are also compared by IF on running the string comparison. So it is needed that both compared strings are enclosed in double quotes.

@echo off
if "%DATE%"=="Sun 10/15/2017" ( echo "Today is Sunday." ) else ( echo "Today is unknown." )
pause

Another solution if region dependent date really contains the abbreviated weekday too.

@echo off
if not "%DATE:Sun=%"=="%DATE%" ( echo "Today is Sunday." ) else ( echo "Today is unknown." )
pause

This batch file compares the current date string with all case-insensitive occurrences of Sun being replaced by nothing with unmodified current date string. The string substitution is successful only on date string containing weekday abbreviation Sun and in this case the two compared strings are not equal.

On my computer the IF condition is never true as %DATE% expands to a date string not containing weekday according to configuration in Windows Region and Language settings.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • if /?
  • set /?
Mofi
  • 46,139
  • 17
  • 80
  • 143