?
Solved

batch file coding help- creating a logfile

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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
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…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

762 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