Need help clearing a log while it is being written to (CMD)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Shahar5
Posts: 2
Joined: 19 May 2021 03:29

Need help clearing a log while it is being written to (CMD)

#1 Post by Shahar5 » 19 May 2021 03:36

Hello DosTips Community

I need a script that transfers a logs content to another and then clears that first log, while the first log is also being filled with logs
I hope I didn't confuse you

what I thought would work is
1. cd "logs location"
2. type firstlog.log >> otherlog.log
3. del /F firstlog.log

the error message i'm getting is
"the process cannot access the file because it is being used by another process"
this is after line 3

again just to clarify because i'm usually not writing so clearly..
I need the firstlog's content to be cleared after each transfer, I tried del /F because echo. > firstlog.log didn't work either
Would love some advice or enlightment, thank you!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Need help clearing a log while it is being written to (CMD)

#2 Post by aGerman » 19 May 2021 14:19

You're likely out of luck unless you can change the behavior of how the process which writes firstlog.log opens the file, or if you terminate this process.

Windows allows a process to open a file in different modes. This does not only affect the own access of the file, but also the access shared with other processes. If the developer of an application decided to avoid that a file is concurrently written by different processes, only read access (or not even that) will be shared with other processes. (Further reading: https://docs.microsoft.com/en-us/window ... reatefilew especially the ducumentation of dwShareMode.) This seems to be the case according to your description.

Steffen

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: Need help clearing a log while it is being written to (CMD)

#3 Post by AR Coding » 19 May 2021 21:08

Hi, im kind of new to batch but i just did sum reseach and thought that maybe you can use dbenham`s :getPID method at viewtopic.php?p=38870#p38870 and then store the Pid var in a tmp file, call the file, taskkill stored pid, then del 1st log
sample (untested):
cd "logs location"
type firstlog.log>>otherlog.log
::use dbenhams method to pid var into tmp file
cmd /c "call pid_tmp.Bat && taskkill /pid %pid% && del firstlog.log"

Shahar5
Posts: 2
Joined: 19 May 2021 03:29

Re: Need help clearing a log while it is being written to (CMD)

#4 Post by Shahar5 » 20 May 2021 02:38

aGerman wrote:
19 May 2021 14:19
You're likely out of luck unless you can change the behavior of how the process which writes firstlog.log opens the file, or if you terminate this process.

Windows allows a process to open a file in different modes. This does not only affect the own access of the file, but also the access shared with other processes. If the developer of an application decided to avoid that a file is concurrently written by different processes, only read access (or not even that) will be shared with other processes. (Further reading: https://docs.microsoft.com/en-us/window ... reatefilew especially the ducumentation of dwShareMode.) This seems to be the case according to your description.

Steffen
Thank you for the answer!, I hoped it would be as easy as some override command but I guess it's not, I'll figure out another way to do what I needed then :)

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Need help clearing a log while it is being written to (CMD)

#5 Post by miskox » 21 May 2021 03:19

Your solution has a problem: if this log file is very large (or not) it can happen that additional information will be appended to it (firstlog file). This means that when TYPE finishes and you try to delete this file after the TYPE command you will lose some information. If you could do this: rename firstlog to some temporary file and then append this file to the otherlog and then delete this temporary file.

Saso

Post Reply