问题描述
我一直在阅读关于检测锁定文件的信息,现在我想在自己的脚本中实现它,因此,我需要一种以编程方式检测它的方法在 Windows Shell脚本中.
公认的解决方案提供了一种使文件保持打开/锁定状态的方法:
I have been reading this thread about detecting a locked file, and now I would like to implement it in my scripts, so I will need a way to programatically detect it in Windows shell scripting.
The accepted solution gives a way to hold a file open/locked:
(
>&2 pause
) >> test.txt
但是此解决方案需要按键.
我想创建一个类似以下内容的 Windows shell脚本:
but this solution needs a key pressing.
I would like to create a Windows shell script similar, like this:
[Create and locking of c:\Temp\ProgramRunning.lck]
--------------- Rest of the Script ---------------
[Blah blah blah]
---------- End of the Main Body the Script ----------
[Unlocking and deletion of c:\Temp\ProgramRunning.lck]
因此,当脚本完成时,锁文件将被删除(当然也被解锁).如果此脚本已停止,Ctrl + C,窗口关闭,系统挂起或其他原因,则不会删除锁定文件,而是未锁定.
So, when the scripts finishes, the lock file gets deleted (and unlocked, of course). If this script gets stopped, Ctrl+C, window closed, hanging of the system or whatever, the lockfile is not deleted, but is unlocked.
我该怎么办?
推荐答案
您可以将call
与输出重定向结合使用,以使文件保持打开状态,直到调用返回:
You can use call
combined with output redirection to hold a file open until the call returns:
call :main 3>myopenfile.txt
goto :eof
:main
rem do whatever here
goto :eof
如果需要,您可以像这样从调用中写入文件:
If needed, you can write to the file from within the call like this:
>>&3 echo hi
这篇关于如何从Windows命令行Shell脚本中打开(锁定)文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!