We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

How to include current date in a batch created file?

Medium Priority
815 Views
Last Modified: 2013-12-28
Hi all,
i need to create a log file when everytime i run my batch file. this log file name should be like: mylog20090930.log. so the formate is mylogYYYYMMDD.log

i did some research and tried the solution, but not get the result i want.
eg. mylog%date:~-4,4%%date:~-7,2%%date:~0,2%.log , it returns mylog200907Fr.log

can anyone help me?
i am using windows server 2003

thanks
viola
my batch file:
 
echo                         >>mylog%date:~-4,4%%date:~-7,2%%date:~0,2%.log 2>&1                             
echo Submission                                   >> mylog%date:~-4,4%%date:~-7,2%%date:~0,2%.log 2>&1
CMTTransToTA.exe			       >> mylog%date:~-4,4%%date:~-7,2%%date:~0,2%.log 2>&1
 
it generate the log file name as  mylog200907Fr.log, i don't know how to fix it.
 
---------------------------------------
the second way i tried is:
echo on
 
FOR /F TOKENS=1* DELIMS=  %%A IN (DATE/T) DO SET CDATE=%%B
FOR /F TOKENS=1,2 eol=/ DELIMS=/  %%A IN (DATE/T) DO SET mm=%%B
FOR /F TOKENS=1,2 DELIMS=/ eol=/ %%A IN (echo %CDATE%) DO SET dd=%%B
FOR /F TOKENS=2,3 DELIMS=/  %%A IN (echo %CDATE%) DO SET yyyy=%%B
SET date=mylog%mm%%dd%%yyyy%.log 
 
                                            
CMTTransToTA.exe			          >> %date% 2>&1
 
there is no log file generated. what's going on?
can anyone give me a simple solution?

Open in new window

Comment
Watch Question

Jesse BSystem Administrator

Commented:
The code I've used fo something similar to this is below.  You can probably just rearrange the ordering of day, month and year.
DATE /T > %CURRDATE%
set PARSEARG="eol=; tokens=1,2,3,4* delims=/, "
for /F %PARSEARG% %%i in (%CURRDATE%) Do SET DDMMYYYY=%%j-%%k-%%l
 
commandtorun.exe >> mylog%DDMMYYYY%.log

Open in new window

Author

Commented:
hi, i tried. but not working, no log file is generated.

thanks
echo on 
DATE /T > %CURRDATE%
set PARSEARG="eol=; tokens=1,2,3,4* delims=/, "
for /F %PARSEARG% %%i in (%CURRDATE%) Do SET DDMMYYYY=%%j-%%k-%%l
 
                                            
 
CMTTransToTA.exe			          >> mylog%DDMMYYYY%.log 2>&1

Open in new window

Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
In your original script it is because you have used "date" as the variable which is always the system date anyway.  The second method looks OK but there is no filenme in %currdate% so the date is placed into nothing and therefore not read back by the for command.  You could use this:

for /F "eol=; tokens=1,2,3,4* delims=/, " %%i in ('date /t') Do SET DDMMYYYY=%%j-%%k-%%l

Though having been bitten too many times by people changing date formats, the OS deciding to revert to different setting after a profile gets deleted etc. that relying on the the output of date/t or %date% is not so good.

Best bet is to grab the parts of the date using a one-line VB Script which can be created on the fly or saved in the same dir as the batch file:

In fact I wrote this article about it yesterday, hope it helps:

https://www.experts-exchange.com/articles/OS/Microsoft_Operating_Systems/MS_DOS/Using-dates-in-batch-files-scripts.html

Steve

Author

Commented:
hi steve,
it is working, but there is an issue: a file is generated but without file extension.
the generated file name is like: mylog10-07-2009

thanks
viola
echo.    
for /F "eol=; tokens=1,2,3,4* delims=/, " %%i in ('date /t') Do SET DDMMYYYY=%%j-%%k-%%l                                         
CMTTransToTA.exe			          >> mylog%DDMMYYYY%.log 2>&1

Open in new window

IT Consultancy
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
The text wrapped in that, so watch that the 2nd line (starting with vlogname=mylog%...) should be a continuation of the line ending with 'do set'.

Commented:
Why not just use the following:

   SET logfile=mylog%date:~-10,2%%date:~-7,2%%date:~-4%
   CMTTransToTA.exe >>%logfile%
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
Qlemo

After confirming, I can only assume I goet side-tracked last night and lost the my thread somewhere. Yes, you are right. The desired output is: YYYYMMDD.

I was looking through the replies to try to establish whether anyone had bothered to ask the asker to confirm the format and region of his date setting. On closer examination (and after some hours of sleep) I can now see his current date format is: ddd DD/MM/YYYY.

Therefore, to get from:

    ddd DD/MM/YYYY

to:

    mylogYYYYMMDD.log

he needs the following code:

    SET logfile=mylog%DATE:~-4%%DATE:~-7,2%%DATE:~-10,2%.log
    CMTTransToTA.exe >>%logfile%


EXPLANATION

The variable 'logfile' is constructed using the following components:

    mylog                       <-- String literal
    %DATE:~-4%          <-- Last four characters of DATE = YYYY
    %DATE:~-7,2%       <-- Two characters of DATE starting 7th character from right = MM
    %DATE:~-10,2%     <-- Two characters of DATE starting 10th character from right = DD
    .log                          <-- String literal
   
If the asker's date format is 'ddd DD/MM/YYYY' then he could also use the following components:

    mylog                       <-- String literal
    %DATE:~-4%          <-- Last four characters of DATE = YYYY
    %DATE:~7,2%        <-- Two characters of DATE starting 8th -1 character from left = MM
    %DATE:~4,2%        <-- Two characters of DATE starting 5th -1 character from left = DD
    .log                          <-- String literal
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
To be fair here he said in the 4th post in this thread.... "it is workin but there is no file extension" which was just down to a load of spaces on the ends of one of the lines.  I like the redirecting an entire section surrounded by ( ) and use that myself.

Commented:
Well, I have to admit there's been a lot of good input into this question and I hope the asker will consider that when he or she shares out the points among us.
 

Author

Commented:
dragon-it: thanks a lot for your help, it works very well.

for the others: thanks a lot.
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Thanks for selecting my answer, Steve
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.