Tuesday, May 14, 2019

Windows Batch (CMD) File Extrapolationg DATE Variables.

If you go to a Command Prompt and type:

echo %DATE%
Your system will respond in the manner in which it's configured based on date format and localization.

My system responds with:


Tue 05/14/2019
To disect this data use:

SET TDATE=%DATE:~10,4%%DATE:~7,2%%DATE:~4,2%

This can be used later like so:


SET FILENAME=EFSSTATS_%TDATE%

To decipher the work, %DATE:~10,4% takes 4 characters starting at position 10:

1 2 3 4 5 6 7 8 9 A B C D E F
------------------------------
T u e   0 5 / 1 4 / 2 0 1 9

The same can be applied to  %DATE:~7,2% and %DATE:~4,2% for logic's sake.


The Caveat (updated 2020-12-14):

Localization will be the undoing of this functionality but Powershell is the solution. When you're in a global environment this is a better choice. 

FOR /F "usebackq delims=" %a in...
(`powershell -C "$(Get-Date).ToString('yyyy-MM-dd')"`) do SET today=%a

Then you can use it:

ECHO %today%
 

Result:
2020-12-14

To go a little further you can create a precise timestamp by using:

'yyyy-MM-ddTHHMMss' which gives me (now ~ GMT) 2020-12-14T033429

Explore your options. Localization is easier with Powershell, but not infallible.

There is no individual ownership when you are part of a team, it's the sum of the parts that makes you the RESILIENT team you need to be.