Solved

batch file coding help- creating a logfile

Posted on 2014-04-30
2
387 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
2 Comments
 
LVL 82

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to dynamically set the form action using jQuery.

707 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

16 Experts available now in Live!

Get 1:1 Help Now