22 Eylül 2012 Cumartesi

Extract %date% in Batch Programming

Hi Friends,

One very annoying problems is extracting date & time using batch programming.
The reason is simple!
Try c:\>echo %date% on Windows 2003 Server v/s Windows 2000 Server
Windows 2000 Server Output: Sun 06/13/2010
Windows 2003 Server Output: 06/13/2010

You will see that both the output are different & that is a cause of big headache. When i write any administrative scripts for taking log backups with appropriate date, it is very important for me that such platform differences should not make my batch scripts to fail on either of the platform.

Thus, this let me out find a generic solution which will always extract correct date for me no matter its Windows 2000 Server or Window 2003 Server as the underlying platform!

My earlier way to extract date was like this:
@set date_temp=%date:~4,2%%date:~7,2%%date:~10,4%
This use to give me output as

Windows 2000 Server Output: 06132010
Windows 2003 Server Output: 3/01

As you can clearly see that my approach fails on Windows 2003 Server platform. I have the option to modify & setup my date extraction as per Windows 2003 Server platform, but that would make it fail on Windows 2000 Server :-(
So how to find that all in one code?

Well, its simple! Just have a glance at the below code:


Above code will always return you the correct date regardless of the underlying platform. It works correctly because its logic is based on the delimiter "-"
I was relieved that, atleast the delimiter is common across all the Windows Versions :-)

I hope this helps you in your batch or DOS programming :-D
If you have similar issues for extracting %time%, checkout how to extract %time%!

Keywords:
how to extract %date%, extract %date%, batch script, batch scripting, batch program, windows scripting, windows scripts, DOS, Windows 2000 Server, Windows 2003 Server, %date%, echo, etcRelated Posts : Misc Info,Programming

Hiç yorum yok:

Yorum Gönder