Imagine that I have a shared folder MyShared:
User A, gets the file \MyShared:\Foo.txt after every 30 seconds.
And I overwrite \MyShared:\Foo.txt also after every 28 seconds (within my PowerShell script)
How can I prevent myself to overwrite this file while the user is getting it, in PowerShell?
(I do not want to break the content of the file or end-up with some error by attempting to overwrite it in the time of user retrieving it)
If I rephrase the question:
How can I force a Powershell script to wait overwriting a file until another process is finished reading it?
解决方案
In some cases, you may not be able to overwrite it if the file is open.
Otherwise, you will have to devise some other mechanism to signal that the reader has finished reading it. This is not really related to powershell. For example, the reader can create a "lock file" to notify the writer that the file is being read, which it deletes after completing the read. The powershell script can delete the file if the lock file does not exist.