Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get m,d,y into separate variables in a batch file

Status
Not open for further replies.

ESquared

Programmer
Dec 23, 2003
6,129
US
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:

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
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:
Code:
echo dow %dayname%
echo month %mm%
echo day %dd%
echo year %yy%
To clear the environment variables:
Code:
set dow=&set mm=&set dd=&set yy=
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:
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
 
If I might suggest a potentially easier way:

First, determine how your regional settings display the date:

Code:
echo %date%

Now, you can take any set of characters by using

Code:
echo %date:~x,y%

Where x is the starting character and y is the number of characters. So, for example, if you have a standard United States based date format of DAY MM/DD/YYYY, you could use:

Code:
REM Starting after the 0 character (beginning of the variable)
REM take 3 characters from the variable
SET DayOfWeek=%date:~0,3%

REM Starting after the 4th character, take 2 characters
SET Month=%date:~4,2%

REM Starting after the 7th character, take 2 characters
SET Day=%date:~7,2%

REM When no starting character is supplied, use a minus sign
REM to take this many characters from the end of the variable.
SET Year=%date:~-4%

You can do similarly using Time.

Another trick to remember, you can use substitution - for example, any variable can have characters replaced with another character. %variable:<replace this character>=<with this character%

So, in practice:
Code:
REM replace "/" characters with "-" characters
echo %date:/=-%

This is especially handy when you want to script a rename of files by date. For example:
Code:
ren "myfile.ext" "%date:/=-% myfile.ext"

Of course, you can use the previous date tips to create variables you can order as you like. (I personally prefer ordering things my YYMMDD.HHMMSS).

For much more information on the ways you can massage environment variables, read over the help information on SET and FOR - at a command prompt, enter SET /? and FOR /?
 
Thanks for the tips, lwcomputing.

The idea from the posts in the web page I referenced was that one doesn't know the regional settings of the computer and what the part order will be, mm-dd-yy or yyyy-mm-dd or dd-mm-yy. Writing a batch file so it is only sure to work well on your own computer and assuming that the settings will never change, what fun is that!?!? :)

So instead of lots of if statements determining which portion of %date% is which part, I thought it would be cool to let the output of the date-setting function set the variables for me, without having to know in advance what order they're in or even how long they are.

So, your way is definitely easier, but with a different goal in mind. And my solution is not necessarily *easy*, as that was not my aim... fun was!

Knowing the date part order in advance makes it easy, and you still don't have to use the substring stuff:
Code:
for /F "tokens=1-4 delims=-/. " %A in ("%date%") do set dow=%A&set month=%B&set day=%C&set year=%D

Again... not particularly easy, unless you're really familiar with the for command. But fun!
 
Shorter:

FOR /f "tokens=1-4 delims=/-. " %%G IN ('date /t') DO (FOR /f "skip=1 tokens=2-4 delims=(-)" %%G IN 'echo.^|date')) DO (set %%G=%1&set %%H=%2&set %%I=%3)

Rob Vanderwoude wrote about this double substitution method several years ago:
@echo off&SETLOCAL

:: This will return date into environment vars
:: Works on any NT/2K/XP machine independent of regional date settings
:: 20 March 2002

FOR /f "tokens=1-4 delims=/-. " %%G IN ('date /t') DO (call :s_fixdate %%G %%H %%I %%J)
goto :s_print_the_date

:s_fixdate
if "%1:~0,1%" GTR "9" shift
FOR /f "skip=1 tokens=2-4 delims=(-)" %%G IN ('echo.^|date') DO (
set %%G=%1&set %%H=%2&set %%I=%3)
goto :eof

:s_print_the_date
echo Month:[%mm%] Day:[%dd%] Year:[%yy%]
ENDLOCAL&SET mm=%mm%&SET dd=%dd%&SET yy=%yy%

:: see also :: ::



____________________________
Users Helping Users
 
Thanks for sharing, bcastner.

I'm not sure what settings are on or off in my system, but when I try your shorter code I get
'echo.|date' was unexpected at this time.

Which is why I used the "usebackq" keyword...
 
FOR /f "tokens=1-4 delims=/-. " %G IN ('date /t') DO FOR /f "skip=1 tokens=2-4 delims=(-)" %G IN ('echo.^|date') DO (set %G=%1&set %H=%2&set %I=%3)


____________________________
Users Helping Users
 
Now,

mm=%1
dd=%2
yy=%3

It's not running inside a batch file, so %# doesn't dereference to anything, so the variables just get set to the %# text.
 
Inside a batch file, the % symbols in the for command must be doubled

FOR /f "tokens=1-4 delims=/-. " %%G IN ('date /t') DO FOR /f "skip=1 tokens=2-4 delims=(-)" %%G IN ('echo.^|date') DO (set %%G=%1&set %%H=%2&set %%I=%3)
 
lwcomputing,

Bcastner's batch file solution is longer, but he proposed that it was shorter. So either he meant it to be a stand-alone statement, or he was somehow confused about how to measure length. The way I interpreted it seemed to give him more credit.

E

P.S. Context and knowing what has gone before are crucial when responding to people. Someone in this thread did say "double up all percent characters for use in a batch file." So that can't be the only problem going on...
 
Although I was very pleased to learn that one can call labels as well as batch files... this I did not know and can definitely use!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top