我怎么能强制PowerShell脚本等待,直到其他进程读完它覆盖文件?脚本、进程、文件、我怎么能

2023-09-03 07:53:39 作者:很是差勁

想象一下,我有一个共享文件夹MyShared:

Imagine that I have a shared folder MyShared:

用户A,获取文件\ MyShared:\ foo.txt的每30秒后

User A, gets the file \MyShared:\Foo.txt after every 30 seconds.

和我覆盖\ MyShared:\ foo.txt的同时每28秒(在我的PowerShell脚本)在

And I overwrite \MyShared:\Foo.txt also after every 28 seconds (within my PowerShell script)

我怎么能prevent自己要覆盖该文件,而用户得到它,在PowerShell中? (我不想通过试图覆盖它在用户检索其时间打破文件或最终与一些错误的内容)

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)

如果我改写的问题: 我怎么能强制PowerShell脚本等待覆盖文件,直到另一个进程读完了吗?

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.

否则,你将不得不制定一些其他的机制,以表示该读者已经读完它。这是不是真的涉及到PowerShell的。例如,读取器可以创建一个锁定文件通知写入该文件被读取,它在完成读出后将其删除。 PowerShell脚本可以删除该文件,如果锁文件不存在。

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.