The renaming method did not work for me (tested on Windows XP SP3). I started an arbitrary application and tried to rename it to myself. There was no mistake, no effect.
However, this method did work:
COPY /B app.exe+NUL app.exe
When the application started, this command caused an error message. And when the application was disconnected, not only this command saved the contents of the file, but also left the timestamp of the change unchanged.
If I were you, I would probably use this command (at the beginning of the script package, as you said) as follows:
COPY /B app.exe+NUL app.exe >NUL || (GOTO :EOF)
Operator || transfers control to the command / block next to it if the previous command did not work (raised the error level value). Therefore, the above command would end the script if COPY failed (what would happen if the file was opened).
The error message will be saved (since such messages are usually sent to the so-called standard error device and are not discarded with >NUL redirection, while other error messages are usually sent to standard output and, therefore, can be suppressed with >NUL ) and serve as an explanation for the premature completion of the script. However, if you want to display your own message, you can try something like this:
COPY /B app.exe+NUL app.exe >NUL 2>NUL || (ECHO Target file inaccessible& GOTO :EOF)
While >NUL hides everything that is sent to standard output, 2>NUL does the same for standard error.
source share