Solved How to write date in batch script?

Microsoft Windows xp inside out, second...
September 17, 2013 at 09:06:44
Specs: Windows XP, pentium 4 3.00Ghz. 1gb ram
How to write following condition in batch script?

If date today is greater than 31/12/2013 or less than 1/1/2013 goto ABCD

Thanks for your support.


See More: How to write date in batch script?

Report •

✔ Best Answer
September 18, 2013 at 17:39:45
Ha ha! yeh, all true.
I figured that the format was "year 3rd" and went with that, (since no actual math is involved, just isolating the year). The O.P. might want to try this, to see if it's not a "delayed expansion" thing:
for /f "tokens=3 delims=/-" %%a in ("%date%") do (
echo %%a
if "%%a" neq "2013" goto :ABCD
)
and check your results.

Aside from that, all the other responses are relevant and critical. Date formats are notoriously flexible and inconsistant, considering different operating systems, different local environments, and different software packages, each of which has its own "favorite" format. Someday it may be universal, but not today! Like razor said, vbscript can usually sort it out:
::==== batchscript snippet
for /f %%a in ('cscript /nologo yearof.vbs') do set yr=%%a
echo "%yr%"
::========= end snip

'====== vbscript "yearof.vbs"
wscript.echo year(date)
'======= end vbscript



#1
September 17, 2013 at 17:22:24
Here's a stab, but I'm making assumptions that may cause failure:

for /f "tokens=3 delims=/-" %%a in ("%date%") do set yr=%%a
if "%yr%" neq "2013" goto :ABCD


Report •

#2
September 18, 2013 at 08:10:29
Thanks nbrane. I tried your script but I fail to figure out why it isn't working.

Report •

#3
September 18, 2013 at 10:04:17
It might help if you copy the output of echo %date% from the command line.

nbrane went with a standard date formula, sometimes its not the same from computer to computer, posting the output would let it get tweaked to be closer to the correct solution.

for example mine is set up Wed 09/18/2013

::mike

message edited by mikelinus


Report •

Related Solutions

#4
September 18, 2013 at 10:41:21
Yeah, why are you using batch? VBScript handles date math, whereas batch does not. You're forced to fake it, and such faking may break down when moved to a different computer, or if you change the regional settings.

How To Ask Questions The Smart Way


Report •

#5
September 18, 2013 at 11:32:03
[1] There is NO standard date layout.

[2] Date math in BAT almost always ends in tears.

=====================
M2 Golden-Triangle


Report •

#6
September 18, 2013 at 17:39:45
✔ Best Answer
Ha ha! yeh, all true.
I figured that the format was "year 3rd" and went with that, (since no actual math is involved, just isolating the year). The O.P. might want to try this, to see if it's not a "delayed expansion" thing:
for /f "tokens=3 delims=/-" %%a in ("%date%") do (
echo %%a
if "%%a" neq "2013" goto :ABCD
)
and check your results.

Aside from that, all the other responses are relevant and critical. Date formats are notoriously flexible and inconsistant, considering different operating systems, different local environments, and different software packages, each of which has its own "favorite" format. Someday it may be universal, but not today! Like razor said, vbscript can usually sort it out:
::==== batchscript snippet
for /f %%a in ('cscript /nologo yearof.vbs') do set yr=%%a
echo "%yr%"
::========= end snip

'====== vbscript "yearof.vbs"
wscript.echo year(date)
'======= end vbscript


Report •

#7
September 18, 2013 at 18:35:26
Thanks to all expert for your valuable suggestions

message edited by Nabam


Report •

Ask Question