Solved

batch file coding help- creating a logfile

Posted on 2014-04-30
2
404 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

739 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