Compare Current Date To A Specific Date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
yerbmeld
Posts: 3
Joined: 22 Mar 2018 09:38

Compare Current Date To A Specific Date

#1 Post by yerbmeld » 22 Mar 2018 10:42

Hello,

We use batch files to start MS Access databases. The batch files are stored in a network folder and copy the front-end user interface database file to the user's computer. We are in the process of converting the MS Access user interface databases to C# applications. I have one application that the administrator wants the transition to occur on May 1, 2018.

How would I compare the current date to May 1, 2018 in a batch file? If the current date is before (less than) May 1, 2018, I want to start the MS Access database. If the current date is on or after (greater than or equal) May 1, 2018, I want to start the C# application.

Code: Select all

echo off
REM -----
REM   modified Ideal ANMI Start-Up Batch for 2007 - can't share due to no D: drive.
REM -----


set appid=eBN


:BEGIN
:: Compare current date to May 1, 2018
:: If current date geq May 1, 2018, goto CSharp

:WIN2007
set appos=WIN2007

if exist "%USERPROFILE%\Desktop\Shortcut to eBN Generator" del "%USERPROFILE%\Desktop\Shortcut to eBN Generator"
if not exist "%USERPROFILE%\Desktop\eBN Generator.lnk" copy "\\snt206\Repository\ICN2007\eBN Generator.lnk" "%USERPROFILE%\Desktop\eBN Generator.lnk" 
if not exist "%USERPROFILE%\apps\%appid%" mkdir "%USERPROFILE%\apps\%appid%"
copy \\snt207\Repository\ICN2007\Install\eBNUser.mde %USERPROFILE%\apps\%appid%\eBNUser.mde >nul
copy "\\snt207\Repository\ICN2007\Install\Cover Sheet.rtf" "%USERPROFILE%\apps\%appid%\Cover Sheet.rtf" >nul
start /MAX msaccess.exe %USERPROFILE%\apps\%appid%\eBNUser.mde

:CSharp

:DONE

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Compare Current Date To A Specific Date

#2 Post by penpen » 22 Mar 2018 11:41

The solution to that issue heavily depends on the date format used by your computers.
If they all have the same date format, then this might help you:

Code: Select all

:: assumed date format is "dd.mm.yyyy"
for /f "tokens=1-3 delims=." %%a in ("%date%") do (
	if "%%~c%%~b%%~a" geq "20180501" (
		goto :CSharp
	)
)
penpen

yerbmeld
Posts: 3
Joined: 22 Mar 2018 09:38

Re: Compare Current Date To A Specific Date

#3 Post by yerbmeld » 22 Mar 2018 13:07

Thank you, penpen!

My date format is mm/dd/yyyy. However, after parsing the date, the expression "%%~c%%~a%%~b" is returning "2018Thu 0322" and jumps to the CSharp section. My test batch follows:

Code: Select all

echo off
:: assumed date format is "mm/dd/yyyy"
for /f "tokens=1-3 delims=/" %%a in ("%date%") do (
	if %%~c%%~a%%~b geq 20180501 (
		echo %%~c%%~a%%~b
		goto :CSharp
	)
)
:Access
ECHO Today is "%%~a/%%~b/%%~c"
EXIT /b
:CSharp
ECHO Starting C# app
exit /b

Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: Compare Current Date To A Specific Date

#4 Post by Thor » 22 Mar 2018 14:30

Try this:

Code: Select all

@echo off
:: assumed date format is "mm/dd/yyyy"
for /f "tokens=1-2 delims= " %%A in ("%date%") do (
   for /f "tokens=1-3 delims=/" %%x in ("%%B") do (
      set "today=%%~z%%~x%%~y"
      if "%%~z%%~x%%~y" geq "20180501" (
         goto :CSharp
      ) else (
         goto :Access
      )
   )
)
exit /b

:Access
ECHO(
ECHO Today is "%today%"
EXIT /b
:CSharp
ECHO Starting C# app
exit /b

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Compare Current Date To A Specific Date

#5 Post by aGerman » 22 Mar 2018 15:42

Code: Select all

 ... in ("%date:* =%") do ...
If there is any string separated by a space then it will be removed this way.
Nevermind. Use WMIC if you want to keep your code language-independent.

Code: Select all

for /f %%i in ('wmic os get LocalDateTime /value') do for /f %%j in ("%%i") do set "%%j"
echo %LocalDateTime:~,8%
Steffen

yerbmeld
Posts: 3
Joined: 22 Mar 2018 09:38

Re: Compare Current Date To A Specific Date

#6 Post by yerbmeld » 23 Mar 2018 06:26

Thank you for the suggestions! Your assistance is greatly appreciated! After some tinkering, here is what I wound up using:

Code: Select all

echo off

FOR /F "tokens=1-4 delims=/ " %%A IN ("%Date%") DO (
SET DayOfWeek=%%A
ECHO The day of the week is %DayOfWeek%
SET Month=%%B
ECHO The month is %Month%
SET Day=%%C
ECHO The day is %Day%
SET Year=%%D
ECHO The year is %Year%
)

:: assumed date format is "mm/dd/yyyy"
	if %Year%%Month%%Day% geq 20180501 (
		echo The date is %Year%%Month%%Day%
		goto :CSharp
	)
::
:Access
ECHO Today is "%Year%%Month%%Day%"
goto :EOF

:CSharp
ECHO Starting C# app

:EOF
exit

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Compare Current Date To A Specific Date

#7 Post by Squashman » 23 Mar 2018 10:55

yerbmeld wrote:
23 Mar 2018 06:26
Thank you for the suggestions! Your assistance is greatly appreciated! After some tinkering, here is what I wound up using:

Code: Select all

echo off

FOR /F "tokens=1-4 delims=/ " %%A IN ("%Date%") DO (
SET DayOfWeek=%%A
ECHO The day of the week is %DayOfWeek%
SET Month=%%B
ECHO The month is %Month%
SET Day=%%C
ECHO The day is %Day%
SET Year=%%D
ECHO The year is %Year%
)
None of these ECHO commands will work.

Post Reply