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
Solved

Insert time and date into log file

Posted on 2014-12-10
10
355 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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 84

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

792 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