• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

Insert time and date into log file

Hey Experts.  I wanted to insert the time and date into a log file that related to when the script was ran.  A format like:  mm/dd/yy xx:xx pm/am

This would be used in a batch script.  Thanks!
0
samiam41
Asked:
samiam41
3 Solutions
 
NVITCommented:
Add this to your .bat file:
echo %date% %time% >> Logfile.txt

Open in new window

0
 
samiam41Author Commented:
This is what I tried but it didn't work

echo ----[Date & Time Stamp]---->> %logfile%
echo %date:~4,2%_%date:~7,2%_%date:~10,4%>> %logfile%
echo %time:~0,2%_%time:~3,2%>> %logfile%
echo. >> %logfile%
0
 
samiam41Author Commented:
NewVillageIT, thanks for responding.  Here is what I get when I use your script.

The system cannot accept the time entered.
Enter the new time:
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
ISGIT2009Commented:
You can use the following to pull out specific parts of the %DATE% and %TIME% variables to get it to a format you want:

So, if the variable %DATE% is equal to "10/12/2014" which is dd/mm/yyy format and %TIME% was 15:49:08.71

In a batch script, you could do the following:

echo DATE: %DATE:~3,2%/%DATE:~0,2%/%DATE:~8,2%
echo TIME: %TIME:~0,2%.%TIME:~3,2%

Open in new window


Would produce:

DATE: 12/10/14
TIME: 15.49

Open in new window

0
 
samiam41Author Commented:
SOB.....  I used [Date & Time Stamp] which caused a problem.  I took the & out and replaced it with "and".  Now it works.
0
 
NVITCommented:
samiam41,

Try setting DT and TM first. Then replace DATE and TIME with DT and TM in your last post to me:
set dt=%date%
set tm=%time%

Open in new window

0
 
oBdACommented:
Your initial version should work; the issue is with the following line:
echo ----[Date & Time Stamp]---->> %logfile%

Open in new window

The ampersand tells the batch interpreter that a new command follows, so it now tries to run the command "Time" (which then produces the error).
Replace the & either with "and", or escape it with a "^":
echo ----[Date ^& Time Stamp]---->> %logfile%

Open in new window

0
 
samiam41Author Commented:
Thanks for the help and teamwork everyone!
0
 
NVITCommented:
samiam41,

I'm glad to have helped.
0
 
samiam41Author Commented:
Greetings!  If anyone has some spare time and would like to earn some more points, I would appreciate your help.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_28601914.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now