I was wondering how I can have a persistent variable using batch, to be deployed in an environment where I can't install extra software without significant overhead.
I want to detect if it is the first time opened. If it was, it would set the variable to First and then run a set line of code, then it would change the variable to Opened. Is there any way I can try to do this, or is it not possible?
I am afraid I don't understand what you mean with "atomic" term... However, I think the code below do what you want:
@echo off
setlocal
call :InitAtomicVar
if "%atomic%" equ "First" (
echo This is the first time this program run!
echo set "atomic=Opened">> "%~F0"
)
echo Do the rest of program business here...
goto :EOF
rem The next lines *must be* the last lines in this file!
:InitAtomicVar
set "atomic=First"
In this method the program add a line to itself the first time it run, so the next times that the program run it will not repeat the initialization step.