Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to include current date in a batch created file?

Posted on 2009-07-09
16
Medium Priority
?
614 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +4
16 Comments
 
LVL 6

Expert Comment

by:Jesse B
ID: 24820072
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
ID: 24820275
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
ID: 24820590
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 6

Author Comment

by:viola123
ID: 24820727
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 1200 total points
ID: 24821007
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 200 total points
ID: 24821050
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 200 total points
ID: 24823297
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
ID: 24823305
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
 
LVL 16

Expert Comment

by:t0t0
ID: 24832765
Why not just use the following:

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

Assisted Solution

by:Qlemo
Qlemo earned 200 total points
ID: 24833837
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
ID: 24833905
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 200 total points
ID: 24834002
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
ID: 24834363
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
ID: 24835184
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
ID: 24836880
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
ID: 24837744
Thanks for selecting my answer, Steve
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Configuring Remote Assistance for use with SCCM
This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
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…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

715 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