Solved

batch file coding help- creating a logfile

Posted on 2014-04-30
2
402 Views
Last Modified: 2014-05-19
Hello- I have a windows batch file that runs on a server using task scheduler.  It copies some files from one server share to another.  That part works fine.  What I would like to be able to have is a log file in the target destination folder that shows when the batch file ran and updates itself as part of the batch file, putting he most recent event at the top

Something simple, lke a text file log.txt which contains:

04/30/2014 08:00 Copy Complete
04/30/2014 07:45 Copy Complete
04/30/2014 07:30 COPY Complete

Can anyone help with the code I would add in the batch file to create the log file (log.txt) if for some reason it does not exist, and then add the current date, time and 'Copy Complete' message to the top line in the text file?

Thanks!
0
Comment
Question by:snyperj
[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
2 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 40032220
It would be a lot easier to add the latest entry at the end, because you can easily append to a file, but not so easily prepend something.
To add the latest file at the beginning:
@echo off
setlocal
set LogFile=C:\Temp\test.log
REM Do something here ...
if not exist "%LogFile%" echo >NUL 2>"%LogFile%"
for %%a in ("%LogFile%") do (ren "%LogFile%" "%%~nxa.tmp")
>"%LogFile%" echo %Date% %Time% COPY Complete
copy /a "%LogFile%" + "%LogFile%.tmp" "%LogFile%" >NUL
del "%LogFile%.tmp"

Open in new window

Compared to adding at the end:
@echo off
setlocal
set LogFile=C:\Temp\test.log
REM Do something here ...
>>"%LogFile%" echo %Date% %Time% COPY Complete

Open in new window

0
 

Author Closing Comment

by:snyperj
ID: 40075245
sorry thought i HAD closed this out.  Worked great.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

761 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