0

Okay, so I'm trying to make a batch file that when run on April 1st, it will do something else. I want it to make sure that it is any year, but the month has to be 4, and the day has to be 1, then it will do something else, but if this is false, then it will not do that. Here's the code I tried:

if ((%month% = 4) + (%day% = 1))
then
ECHO Test Successful 2
else
ECHO Test Successful 1

I'm new to batch, by the way. I just freestyled this to see if it worked. It gave me a Syntax error.

Syntax Error Can anyone help me?

  • 1
    The proper syntax for the `IF` command can be learned by reading the help file. Open up a cmd prompt and type: `IF /?`. – Squashman Apr 01 '21 at 15:50
  • Does this answer your question? [How to use if - else structure in a batch file?](https://stackoverflow.com/questions/11081735/how-to-use-if-else-structure-in-a-batch-file) – Squashman Apr 01 '21 at 16:00
  • Possible in batch, but unreliable. With extensions enabled, date /T gives the system date, but with the current locales and you have no control on the format except by altering the current locales... Anyway, you could go for a `FOR /F %%D IN ('DATE /T') DO ( SET "XTE=%%D" & IF "%XTE:~0,5%" == "04/01" ( echo APRILFIRST ) )` kind of thing.. Check [this](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format) for better alternatives. – Zilog80 Apr 01 '21 at 16:21

2 Answers2

0

There are several different methods already posted throught the tagged area of this site, which can determine the individual elements of a date. The following example uses just one of them, and will print April Fools!, only if the date is the first day of the fourth month of any year.

@For /F "Tokens=1-3 Delims=/ " %%G In ('""%SystemRoot%\System32\Robocopy.exe" \: . /NJH /L | "%SystemRoot%\System32\find.exe" " 123""') Do @If %%H%%I Equ 0401 (Echo April Fools!) Else Echo Hello.
Compo
  • 36,585
  • 5
  • 27
  • 39
0

This is a way how you can do it, if you also want to check a specific year just enter set year=%date:~6,9%

This will check the day and the month if both of them are equal to the value you set for them, in this case 1 and 4 it will display a message.

set month=%date:~4,1%
set day=%date:~1,1%
If %day% == 1 if %month% == 4 echo April Fools!
  • 1
    Your example code will only work in PC's configured with the date to output beginning with the format dd/MM, dd-MM, or similar. There are a great many locales and PC's, (probably a majority), which do not do so. – Compo Apr 02 '21 at 00:24
  • 1
    What happens when the day of the months is 11 or 21? – Squashman Apr 02 '21 at 06:24