Solved

Insert time and date into log file

Posted on 2014-12-10
10
351 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 23

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
 

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
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.

 
LVL 23

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 23

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
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…
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.

910 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

24 Experts available now in Live!

Get 1:1 Help Now