Replacing a locked file
Posted: 17 Oct 2023 12:48
Hello,
I've got a problem of my own making. A while back I created a bit of a monstrosity of a batch file to automate some tasks and I added this to stop multiple instances.
Credit: https://stackoverflow.com/questions/529 ... t-up-again
Part of my batch file allowed for a future update to it and files it comes with by downloading an update pack and replacing files included as needed. The monstrosity will download a zip file and another batch file to do the update to a temporary location and then the monstrosity launches the downloaded update script using "start" without "/wait" in a new window and exits.
The update script works fine right up until it comes to replacing the file containing the code above, despite the launching batch file having exited the file is still locked somehow and I can't replace it. Once I close the newly launched window that is running the update batch I can edit the monstrosity again, I can't find an alternative way of launching the update script that doesn't seem to inherit the lock I placed on the monstrosity though.
Any ideas?
Thanks!
I've got a problem of my own making. A while back I created a bit of a monstrosity of a batch file to automate some tasks and I added this to stop multiple instances.
Code: Select all
@echo off
9>&2 2>nul (call :lockAndRestoreStdErr %* 8>>"%~f0") || (
echo Only one instance allowed - "%~f0" is already running >&2
pause
)
exit /b
:lockAndRestoreStdErr
call :main %* 2>&9
exit /b 0
:main
echo Do Stuff....
Part of my batch file allowed for a future update to it and files it comes with by downloading an update pack and replacing files included as needed. The monstrosity will download a zip file and another batch file to do the update to a temporary location and then the monstrosity launches the downloaded update script using "start" without "/wait" in a new window and exits.
The update script works fine right up until it comes to replacing the file containing the code above, despite the launching batch file having exited the file is still locked somehow and I can't replace it. Once I close the newly launched window that is running the update batch I can edit the monstrosity again, I can't find an alternative way of launching the update script that doesn't seem to inherit the lock I placed on the monstrosity though.
Any ideas?
Thanks!