I saw some neat batch file tips and tricks at I wanted to add my own contribution, but you have to register to post, and I couldn't quickly find how to register. So I'm posting this here.
Here's how to get the date parts into separate environment variables in a single statement! This should be tolerant of alternate date orders, and doesn't care what separator character is used. Note this is all one line:
Double up all percent characters for use in a batch file. You can remove the @ symbols if you want to see output echoed or if you want to put "@echo off" at the top of your batch file instead.
To see the resulting environment variables:
To clear the environment variables:
or in a batch file just use SETLOCAL and ENDLOCAL.
A slightly longer version, in case something goes wrong with date orders with the top one:
Erik
Here's how to get the date parts into separate environment variables in a single statement! This should be tolerant of alternate date orders, and doesn't care what separator character is used. Note this is all one line:
Code:
@for /F "eol=T usebackq tokens=2-4 delims=()-" %A IN (`"echo. | date"`) do @for /F "tokens=1-4 delims=/-. " %E IN ("%date%") do @set dow=%E&set %A=%F&set %B=%G&set %C=%H
To see the resulting environment variables:
Code:
echo dow %dayname%
echo month %mm%
echo day %dd%
echo year %yy%
Code:
set dow=&set mm=&set dd=&set yy=
A slightly longer version, in case something goes wrong with date orders with the top one:
Code:
@for /F "eol=T usebackq tokens=2-4 delims=()-" %A IN (`"echo. | date"`) do @for /F "tokens=1-4 usebackq delims=/-. " %E IN (`date /T`) do @set dow=%E&set %A=%F&set %B=%G&set %C=%H
Erik