0

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?

Charles Duffy
  • 257,635
  • 38
  • 339
  • 400
  • 1
    I think you mean "persistent", not "atomic". – indiv Jan 18 '18 at 18:14
  • When editing a question, there's no need for edit markers -- the edit history is always publicly visible, so it's more important to make the edited post flow well and be as clear as possible about what is being asked than to make it obvious what was edited when -- anyone who wants to know that history can just read the diffs. – Charles Duffy Jan 19 '18 at 17:07
  • (*Usually*, an edit that changes the question's meaning would be unwelcome after answers were present, and the advice would be to ask a new question to avoid invalidating those prior answers -- but since the only answer you had correctly surmised your actual intent, the change from "atomic" to "persistent" is a fine one). – Charles Duffy Jan 19 '18 at 17:08
  • OK. Thank you Charles. – XXMASTER T518XX Jan 19 '18 at 17:09

1 Answers1

1

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.

Aacini
  • 61,954
  • 12
  • 66
  • 99