Page 1 of 1
Need help clearing a log while it is being written to (CMD)
Posted: 19 May 2021 03:36
by Shahar5
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!
Re: Need help clearing a log while it is being written to (CMD)
Posted: 19 May 2021 14:19
by aGerman
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
Re: Need help clearing a log while it is being written to (CMD)
Posted: 19 May 2021 21:08
by AR Coding
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"
Re: Need help clearing a log while it is being written to (CMD)
Posted: 20 May 2021 02:38
by Shahar5
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

Re: Need help clearing a log while it is being written to (CMD)
Posted: 21 May 2021 03:19
by miskox
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