Solved

Insert time and date into log file

Posted on 2014-12-10
10
353 Views
Last Modified: 2015-01-22
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
Comment
Question by:samiam41
10 Comments
 
LVL 24

Accepted Solution

by:
NVIT earned 167 total points
ID: 40491571
Add this to your .bat file:
echo %date% %time% >> Logfile.txt

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
ID: 40491623
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
 
LVL 9

Author Comment

by:samiam41
ID: 40491627
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Assisted Solution

by:ISGIT2009
ISGIT2009 earned 166 total points
ID: 40491641
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
 
LVL 9

Author Comment

by:samiam41
ID: 40491651
SOB.....  I used [Date & Time Stamp] which caused a problem.  I took the & out and replaced it with "and".  Now it works.
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40491652
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
 
LVL 83

Assisted Solution

by:oBdA
oBdA earned 167 total points
ID: 40491654
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
 
LVL 9

Author Closing Comment

by:samiam41
ID: 40491689
Thanks for the help and teamwork everyone!
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40492317
samiam41,

I'm glad to have helped.
0
 
LVL 9

Author Comment

by:samiam41
ID: 40564733
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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
You may have a outside contractor who comes in once a week or seasonal to do some work in your office but you only want to give him access to the programs and files he needs and keep privet all other documents and programs, can you do this on a loca…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

813 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

17 Experts available now in Live!

Get 1:1 Help Now