I have a simple batch process running once per day to manage a disk quota (simply deletes all folders in excess of quota, oldest first). Works fine for what it's designed to do. Among other tasks, it appends a list of the deleted folders to a log file (a simple text file).
To prevent inadvertent changes to the log file, I have the read-only flag set (not attempting any real security, just protecting against a reflexive "save changes" if viewed in Notepad, for example). It's simple to use ATTRIB to turn the "R" off at the start & back on again at the end of the process.
I notice, however, that Windows doesn't appear to provide write access control to text files: I find that multiple instances of Notepad (or Notepad and another text editor; or a text editor and a CMD window) can each "save" to the text file (if read-only is off)--meaning those inadvertent changes (or wholesale deletion) could happen while the file is "vulnerable" because the process is running & it has the read-only turned off.
OK, it's not that big a deal for this specific application (the process typically takes ~30 seconds to run, and I'm the only user with access and "theoretically" know better)--but, I'd like to understand how to deal with this.
How can I save (append) to a read-only log file without opening it up to any other writes? I'd like something along the lines of:
take exclusive write control of log file
turn off read-only
perform rest of script
turn on read-only
release exclusive write control
It's those first & last steps I can't figure out....