0

I need an if else statement that aborts or continues the batch scripts.

if current date is between specified 2 dates it continues to work if not, batch file aborts

for example i set the dates as 01.01.2019 to 01.03.2019, if the date is 15.02.2019 batch file works if 01.04.2019 it aborts

the closest answers are from here; In a batch file, how would I check if today's date is after a set date?

@ECHO OFF

SET FirstDate=2015-01-24

REM These indexes assume %DATE% is in format:
REM   Abr MM/DD/YYYY - ex. Sun 01/25/2015
SET TodayYear=%DATE:~10,4%
SET TodayMonth=%DATE:~4,2%
SET TodayDay=%DATE:~7,2%

REM Construct today's date to be in the same format as the FirstDate.
REM Since the format is a comparable string, it will evaluate date orders.
IF %TodayYear%-%TodayMonth%-%TodayDay% GTR %FirstDate% (
    ECHO Today is after the first date.
) ELSE (
    ECHO Today is on or before the first date.
)


and / or

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS

REM -- 2 digit day
SET "_day=%DATE:~-10,2%" & REM day goes first (dd/mm/yyyy); if not, remove this line
SET "_day=%DATE:~-7,2%" & REM day goes second (mm/dd/yyyy); if not, remove this line
REM -- 2 digit month
SET "_month=%DATE:~-10,2%" & REM month goes first (mm/dd/yyyy); if not, remove this line
SET "_month=%DATE:~-7,2%" & REM month goes second (dd/mm/yyyy); if not, remove this line
REM -- 4 digit year
SET "_year=%DATE:~-10,4%" & REM year goes first (yyyy/##/##); if not, remove this line
SET "_year=%DATE:~-4%" & REM year goes last (##/##/yyyy); if not, remove this line

REM -- The variables below are set to year-month-day without separators (yyyymmdd)
SET "today=%_year%%_month%%_day%" & REM today's date based on your selections above
SET "compareDate=20180727" & REM the date you are comparing with today

REM -- Here's where the magic happens with comparing the two dates
IF %compareDate% LSS %today% ECHO The comparison date is in the past.
IF %compareDate% EQU %today% ECHO The comparison date is today.
IF %compareDate% GTR %today% ECHO The comparison date is in the future.
GOTO :EOF
  • 2
    Without even an own code try the question is too broad. Hint when reversing the order to yyyy.mm.dd a simple string comparison with an [if](http://ss64.com/nt/if.html) should suffice. –  May 27 '19 at 16:51

1 Answers1

1

The problem with %DATE% is that it returns the current date using the short date format, that is fully (endlessly) customizable. One user may configure its system to return Fri053119, and another user may choose 31/05/2019, which is a complete nightmare for a BAT programmer.

One possible solution is to use WMIC, the command line interface to WMI. See wikipedia's Windows Management Instrumentation.

wmic OS Get localdatetime returns the date in a convenient format to directly parse it with a FOR command.

So, for example, you could,

for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set wldt=%%a
set today=%wldt:~0,8%

to obtain the current date in a convenient format to use it for dates comparison

so you later can

if %today% gtr 20190101 (
  if %today% lss 20190630 (
    echo Yes!
  )
)
PA.
  • 28,486
  • 9
  • 71
  • 95
  • 1
    You shouldn't support double postings with double answers. –  May 31 '19 at 14:18
  • Well, you answered both and temporarily got rep before the other answer was deleted by moderators with the comment: Please don't add the [same answer to multiple questions](http://meta.stackexchange.com/questions/104227/is-it-acceptable-to-add-a-duplicate-answer-to-several-questions). Answer the best one and flag the rest as duplicates. If it is not a duplicate, tailor the post to the question and flag for undeletion. –  May 31 '19 at 18:57
  • A question on hold doesn't remove the answer, so there is no reason to post another answer, a comment referencing the other is the better way. OP notices it anyway. –  May 31 '19 at 19:09