At first I was skeptical about this, but it turns out that this can be done using file redirection. Consider this example:
@echo off
if '%1' == '-lock' (
shift
goto :main
)
call %0 -lock > lockfile.txt
goto :eof
:main
echo %DATE% %TIME% - start
TREE C:\
echo %DATE% %TIME% - finish
goto :eof
While the running batch is running, it is not possible to remove lockfile.txt.
Essentially, batch checking for the '-lock' parameter. If it is absent, it re-executes itself with the -lock option and redirects its own output to the lockfile.txt file
It is also possible to create locks for βcriticalβ partitions within a batch, for example.
@echo off
echo %DATE% %TIME% - started
(
echo Starting TREE
tree c:\
echo TREE finished
) > lock2.lock
echo %DATE% %TIME% - finished
Sources:
Windows?
http://www.dostips.com/forum/viewtopic.php?p=12454