Solved

How to include current date in a batch created file?

Posted on 2009-07-09
16
580 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

0
Comment
Question by:viola123
  • 4
  • 4
  • 3
  • +4
16 Comments
 
LVL 6

Expert Comment

by:jpboyce
Comment Utility
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

0
 
LVL 6

Author Comment

by:viola123
Comment Utility
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

0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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:

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

Steve
0
 
LVL 6

Author Comment

by:viola123
Comment Utility
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

0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 300 total points
Comment Utility
That is because
a) There is some spaces at the end of the for line I hadn't noticed when I copy/pasted so the DDMMYYYY variable is actually set to something like this (between the x's):
X10-07-2009     X

Also you don't have " " around the filename so it cuts off at the first space.

I suggest what you do in general is:

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

etc.  then you can just use >>%logfile% where you want to redirect.  One thing to note with your original code btw was you were using %date:~.....% in each log write.  Main issue with that comes when you run your code at 11:30 at night and it runs over into the next day etc.... i.e. then one date command returns 10-07-2009 and a later one 11-07-2009.  Using a variable like suggested here means you use the same logfile throughout your batch file.

hth

Steve
0
 
LVL 21

Assisted Solution

by:AmazingTech
AmazingTech earned 50 total points
Comment Utility
You were on the right track just a minor adjustment and your own solution would work.

If you like the dashes.

mylog%date:~-4,4%-%date:~-7,2%-%date:~-10,2%.log
echo                         >>mylog%date:~-4,4%%date:~-7,2%%date:~-10,2%.log 2>&1                             

echo Submission                                   >> mylog%date:~-4,4%%date:~-7,2%%date:~-10,2%.log 2>&1

CMTTransToTA.exe			       >> mylog%date:~-4,4%%date:~-7,2%%date:~-10,2%.log 2>&1

Open in new window

0
 
LVL 3

Assisted Solution

by:cclassen
cclassen earned 50 total points
Comment Utility
How about


for /f "tokens=2 delims= " %%i in ('date /t') do for /f "tokens=1,2,3 delims=// " %%j in ("%%i") do set vlogname=mylog%%j%%k%%l.log
echo                         >> %vLogname%                            
echo Submission                                   >>  %vLogname%
CMTTransToTA.exe                         >>  %vLogname%
0
 
LVL 3

Expert Comment

by:cclassen
Comment Utility
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'.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Why not just use the following:

   SET logfile=mylog%date:~-10,2%%date:~-7,2%%date:~-4%
   CMTTransToTA.exe >>%logfile%
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 50 total points
Comment Utility
t0t0,
because the format should be yyyymmdd, with %date% as Wd, mm/dd/yyyy. And a empty line, followed by "Submission" line, followed by the command output, should be logged.

viola123,
I suppose your first echo should create a empty line. If you use echo without something behind, it will output "Echo is on".
I follow AmazingTech's approach, but slightly adapted, as I don't like the repetition of the file redirection:


>> mylog%date:~-4,4%%date:~-7,2%%date:~-10,2%.log 2>&1 (

 echo.

 echo Submission

 CMTTransToTA.exe

)

Open in new window

0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
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
0
 
LVL 16

Assisted Solution

by:t0t0
t0t0 earned 50 total points
Comment Utility
Qlemo, I did not include the other two lines as I was concentrating on the format of the filename however, I reckon the following should do the trick quite nicely:

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

    ECHO. >>%logfile% 2>&1
    ECHO Submission >%logfile% 2>&1
    CMTTransToTA.exe >>%logfile% 2>&1

And I think that just about wraps it up althought it's doubtful you'll really need to redirect STDERR on ALL occasions.

viola123, it is better to assign the filename to a variable if you intend to use it more than once. The reasons are:

   (1) It's easier to correct a single mistake should there be any, rather than sift through multiple lines of code.

   (2) If one of the flenames differ slightly then output will go to different files.

   (3) The system won't have to re-calculate the filename everytime you use it, which is a saving in processing time.

   (4) It reduces the size of your code.

   (5) It makes your code more readable and more easily recognisable as to what it's doing.

Normally, I like Qlemo's approach however, this time I feel he could have done it like this instead:

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

    >>%logfile% 2>&1 (
        ECHO.
        ECHO Submission
        CMTTransToTA.exe
    )

or equally, like this:

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

    (
        ECHO.
        ECHO Submission
        CMTTransToTA.exe
    ) >>%logfile% 2>&1


Whichever the asker finds easier on the eye.

The 'SET logfile...' declaration should be placed somewhere near the top of the code alongside other variable declarations, and out of the way from the main code itself.
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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.
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
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.
 
0
 
LVL 6

Author Comment

by:viola123
Comment Utility
dragon-it: thanks a lot for your help, it works very well.

for the others: thanks a lot.
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Thanks for selecting my answer, Steve
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The use of stolen credentials is a hot commodity this year allowing threat actors to move laterally within the network in order to avoid breach detection.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now