Computer Hope

Microsoft => Microsoft DOS => Topic started by: MacIreland on January 26, 2012, 05:32:01 PM

Title: Batch File to empty contents of log file but not delete it
Post by: MacIreland on January 26, 2012, 05:32:01 PM
Hi,

I have been searching through this forum for a few hours, some great advice on it, I found it very helpful.

I have a program that has a log file for specific alerts.
This log file has the format applogfile120127.log it's name changes every day.
If I delete the log file it doesn't get recreated when a new alert comes in, ( You have to restart the software ) so I would like to be able to delete the contents of this log file but not delete the entire file itself.

Can this be done via a Dos batch file.
My computer is running Win XP Pro

Many thanks

Mac
Title: Re: Batch File to empty contents of log file but not delete it
Post by: MacIreland on January 26, 2012, 11:37:46 PM
As my log file name changes every day, even if I could clear the contents of all log file in a specific directory it would be great.

Thanks

Mac
Title: Re: Batch File to empty contents of log file but not delete it
Post by: Salmon Trout on January 26, 2012, 11:55:38 PM
Try this (substitute the actual file name for "filename.log")

Code: [Select]
echo. > filename.log

Title: Re: Batch File to empty contents of log file but not delete it
Post by: MacIreland on January 27, 2012, 12:52:50 AM
That should work but my filename changes every day. Is there a way around this?
Title: Re: Batch File to empty contents of log file but not delete it
Post by: MacIreland on January 27, 2012, 08:53:37 AM
Is it possible to do a Null on ALL .log files in a directory as the filename is unknown.

If that is not possible if I could read the unknownfilename.log ( but the only logfile in that directory ) and put the last line of that file into a seperate txt file.

Seem like a long way of doing something simple though. the problem is that the file name is unknown as it increments its name every day using the current date. Clearing all log files in the directory should do the job.

Thanks
Title: Re: Batch File to empty contents of log file but not delete it
Post by: Squashman on January 27, 2012, 11:11:20 AM
Not quite sure why you would want to clear the contents of a log file.  Isn't the purpose of a log file to capture relevant information that you can go back and look at another day?
Title: Re: Batch File to empty contents of log file but not delete it
Post by: Squashman on January 27, 2012, 11:27:26 AM
If you really want to clear the contents of all the log files in a directory.
Code: [Select]
@echo off
for %%G in (*.log) do (copy /Y nul "%%G")
Just to show you that it works
Code: [Select]
H:\logfiles>dir /a-d *.log
 Volume in drive H is DATA
 Volume Serial Number is D2F3-49FA

 Directory of H:\logfiles

01/27/12  12:21 PM                16 logfile1.log
01/27/12  12:21 PM                16 logfile2.log
01/27/12  12:21 PM                16 logfile3.log
               3 File(s)             48 bytes
               0 Dir(s)  58,354,429,952 bytes free

H:\logfiles>clearlog.bat
        1 file(s) copied.
        1 file(s) copied.
        1 file(s) copied.

H:\logfiles>dir /a-d *.log
 Volume in drive H is DATA
 Volume Serial Number is D2F3-49FA

 Directory of H:\logfiles

01/27/12  12:24 PM                 0 logfile1.log
01/27/12  12:24 PM                 0 logfile2.log
01/27/12  12:24 PM                 0 logfile3.log
               3 File(s)              0 bytes
               0 Dir(s)  58,354,429,952 bytes free
Title: Re: Batch File to empty contents of log file but not delete it
Post by: MacIreland on January 27, 2012, 12:41:05 PM
Thanks I'll give that a go later.  The reason I want to clear the logs is because I'm trying to capture a line in it and get it to send an email alert. I can only manage to capture the first line, so I am going to get the log file cleared after every email is sent.
Title: Re: Batch File to empty contents of log file but not delete it
Post by: Squashman on January 27, 2012, 02:28:29 PM
We can help you capture a line of text from a log file. That isn't that hard.
Title: Re: Batch File to empty contents of log file but not delete it
Post by: MacIreland on January 27, 2012, 03:59:14 PM
That worked a treat guys, thanks very much for all your efforts :)
You are a credit to this forum
Title: Re: Batch File to empty contents of log file but not delete it
Post by: Migxi on February 02, 2012, 06:32:07 PM
And... won't you prefer just to get the content of the last line ??? That will solve your problem and keep a real log and getting the info you are looking for to email.

Rem Count the number of lines in the file

Set LINES=0
FOR /F "delims==" %%I in (Your_File.log) Do Set /a LINES=LINES+1
REM Get the Last line
More +%LINES% < Your_File.log > Last_Line.txt

Rem Last_Line.txt will have only the last line
Title: Re: Batch File to empty contents of log file but not delete it
Post by: Squashman on February 02, 2012, 08:00:48 PM
And... won't you prefer just to get the content of the last line ??? That will solve your problem and keep a real log and getting the info you are looking for to email.

Rem Count the number of lines in the file

Set LINES=0
FOR /F "delims==" %%I in (Your_File.log) Do Set /a LINES=LINES+1
REM Get the Last line
More +%LINES% < Your_File.log > Last_Line.txt

Rem Last_Line.txt will have only the last line
Two things not quite right about your approach to that. 

1) If you are already going to to use a for loop to count the lines you might as well just use the For loop variable to assign the lines to a variable.  Then you don't need to use the counter at all.  You are already parsing every line.  Might as well use it to your advantage.
Code: [Select]
FOR /F "delims==" %%I in (Your_File.log) Do Set lastline=%%I
2) Using the FIND command is much much much faster at counting the number of lines in a file.