1748142 Members
3639 Online
108758 Solutions
New Discussion юеВ

%Date%

 
SOLVED
Go to solution
Monty Lovell
Advisor

%Date%

Hi I'm trying to write a batch file that will give me today's date not the day itself. (i.e. 061504) Thanks. Monty.
7 REPLIES 7
Ganesh Babu
Honored Contributor
Solution

Re: %Date%

check this out..

for /f "tokens=2,3,4 delims=/ " %%i in ('date /t') do (
set year=%%k
set month=%%i
set day=%%j
)

echo %month%%day%%year%

Ganesh
Monty Lovell
Advisor

Re: %Date%

Thanks Ganesh. I was wondering if you could help me place that syn. within this batch file. I'm trying to get a .zip file made with todays date in it. Here's what I have so far:
cd\
cd c:\Program Files\Winzip
wzzip c:\backup\c%Date%.zip -r -p -t c:\Extra\*.*

So are you saying token should take the place of %date%

Thanks Again for your help in this.
Monty.
Ganesh Babu
Honored Contributor

Re: %Date%

Hi,
What i have posted earlier is how to pharse the date, month and year from the date and use it in the format u needed...

for your requirement it can be as follows..

cd\
cd c:\Program Files\Winzip

for /f "tokens=2,3,4 delims=/ " %%i in ('date /t') do (
set year=%%k
set month=%%i
set day=%%j
)

wzzip c:\backup\c%month%%day%%year%
.zip -r -p -t c:\Extra\*.*

Hope this helps..

Ganesh


Note:- Since u r new forums user.. u should also know about the point system..

http://forums1.itrc.hp.com/service/forums/helptips.do?#28

Jon Finley
Honored Contributor

Re: %Date%

Hey! ..... that's MY code! :-)

If you're running 2000 or XP you can also use the internal variable %date% or %time% in your batch file.

for /f "tokens=2,3,4 delims=/ " %%i in ('echo %Date%') do (
set year=%%k
set month=%%i
set day=%%j
)
set tday=%month%%day%%year%

for /f "tokens=1,2,3 delims=:." %%i in ('echo %time%') do (
set hr=%%i
set min=%%j
set sec=%%k
)
set rnow=%hr%%min%%sec%

set outfile=mydata_%tday%_%rnow%.log

echo This is a log entry. >> %outfile%

or

echo Log entry created on %tday% at %rnow% >> mylogfile.log

Otherwise you have to use the date/t and time/t entries.

Jon
"Do or do not. There is no try!" - Yoda
Ganesh Babu
Honored Contributor

Re: %Date%

Hi Jon,
I learnt about this statement from one of my senior administator before 2 yrs.. may be it is a kind of code known to many of the admins.

Ganesh
Jon Finley
Honored Contributor

Re: %Date%

The line:
for /f "tokens=2,3,4 delims=/ " %%i in ('echo %date%') do (

does the following:
first the FOR statement looks at the delimiters '/' and ' ' (space) We have to take the output from the date command or variable and break it into usable chunks.
Tue 06/15/2004 is how the date displays. If we use a space as a delimiter, we can break off 'Tue' as one entry, then using the '/', break out '06' '15' and '2004'. Tokens refers to the position to start collecting the pieces from. In this case, we don't want 'Tue', so we start at the second position and take the three date parts (2,3,4) for month, day, year.

Jon
"Do or do not. There is no try!" - Yoda
Monty Lovell
Advisor

Re: %Date%

Thanks guys you ROCK!!! =)