Solved

Simple batch script for check on date

Posted on 2014-11-16
5
193 Views
Last Modified: 2014-11-16
Hi Guys,

I would like to create a simple batch file which will do the following -
If today's date is greater than 14/11/2014 then execute a following task.

I have tried the following

IF "%Date%" GTR "17/11/2014" ECHO YES

But this doesn't seem to work and will echo yes all the time.
Any help would be appreciated and I dont want to use VBSCRIPT.
0
Comment
Question by:manav08
  • 3
  • 2
5 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 40445582
Two issues are causing that. First you are comparing two strings.
Next, %date% often has the weekday prefixed.

For a DOS-only solution it is necessary to extract the date parts and compare tbem correctly:
  if "%date:~-4%%date:~-10,2%%date:~-7,2%" GTR "20141114" echo yepp
0
 
LVL 11

Author Comment

by:manav08
ID: 40445586
Hi Qlemo,

We use UK DATE FORMAT i.e. 14/11/2014.
So when I type %DATE% in command prompt, I get the following : Sun 16/11/2014

How will I change your script please?
0
 
LVL 11

Author Comment

by:manav08
ID: 40445590
actually when I typed
echo %date:~-4%%date:~-10,2%%date:~-7,2%
the output was 20141611

This technically means that 20141512 is less than 20141611, which is not actually true :(
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40445598
Of course, I did not spot you are using a "correct" date format, which is at least consequently "wrong" in regard of sorting, and not that mixed-up stuff of US :D

Each %date....% is one date part. So just arrange them different, and you are set:
  if "%date:~-4%%date:~-7,2%%date:~-10,2%" GTR "20141114" echo yepp
0
 
LVL 11

Author Closing Comment

by:manav08
ID: 40445623
You are a scripting genius. Thank you
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Resolve DNS query failed errors for Exchange
A procedure for exporting installed hotfix details of remote computers using powershell
In this Micro Tutorial viewers will learn how to restore single file or folder from Bare Metal backup image of their system. Tutorial shows how to restore files and folders from system backup. Often it is not needed to restore entire system when onl…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question