Clear Contents of a Log File using Batch Script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mylehari
Posts: 2
Joined: 13 Jan 2010 16:02

Clear Contents of a Log File using Batch Script

#1 Post by mylehari » 13 Jan 2010 16:11

Hello,

I am trying to clear the contents of a log file app1.log which is being used by a server.
I am using windows 2003.

Following are the commands I tried so far.

command: echo on>app1.log
output: The process cannot access the file because it is being used by another process

command:type nul>app1.log
output: The process cannot access the file because it is being used by another process

command:type app1.log>NUL>app1.log
output: The process cannot access the file because it is being used by another process

I am able to open the file, select content,delete and save the file under same name - manually.
Can some one Please suggest how to put this in a batch script.

Thank You.

sweener
Posts: 18
Joined: 06 Jan 2010 13:00
Location: Indianapolis, IN

#2 Post by sweener » 14 Jan 2010 08:59

Was it just a command process that piped into the log? What you have is potentially a prompt like 'Overwrite File(Y/N)?' in a background process, so what I have done is use TSKILL after finding the PID associated with the log. You can try QUERY PROCESS * to list all active processes and their IDs (Not sure if this works in Server 2003) If you see a suspicious 'cmd.exe' you can TSKILL <PID> where PID is the PID associated with the hanging image.

If TSKILL doesn't work, try TASKKILL and if QUERY isn't a recognized command, try QPROCESS

I can try and provide an example if you need one. Let me know!

mylehari
Posts: 2
Joined: 13 Jan 2010 16:02

#3 Post by mylehari » 14 Jan 2010 11:02

Sweener,
Thanks for replying

what I am looking for is just log rolling..
since my log wouldnt roll by itself, I want to write a batch to copy the contents to another location and clear the original log to prevent it from
growing huge.

I am sure there is a way to do this without killing the PID.
I have admin access on this m/c so am sure its not any access issue.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

#4 Post by alan_b » 16 Jan 2010 05:24

Try this :-

Code: Select all

START CMD.EXE /C "ping -w 40 -n 100 127.0.0.1 >> test.txt"


Whilst the second CMD was running "ping", I gave the primary CMD two commands :-
C:\Documents and Settings\Dad>type test.txt

Pinging 127.0.0.1 with 32 bytes of data:

Reply from 127.0.0.1: bytes=32 time<1ms TTL=64
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64
Reply from 127.0.0.1: bytes=32 time<1ms TTL=64

C:\Documents and Settings\Dad>del test.txt
C:\Documents and Settings\Dad\test.txt
The process cannot access the file because it is being used by another process.


From that you can see that whilst ping is running, I am allowed to TYPE the contents of test.txt;
but I am NOT allowed to delete the file, even though ping probably spends less than 1 mSec actually writing an update.

Obviously when ping output is directed to a file then ping has exclusive "write" authority over that file, even during the 999 mSeconds intervals during which it is not writing. The file can only be deleted when ping exits.

Your server log files are probably subject to similar restrictions.

Alan

Post Reply