Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

batch file coding help- creating a logfile

Posted on 2014-04-30
2
Medium Priority
?
412 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 85

Accepted Solution

by:
oBdA earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

610 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