Store various information in a batch notebook

0

Good morning, I'll contextualize first. Basically I'm doing an "interactive story", where basically, during the progress of the story I'll have 3 variables. I could save these variables with the set command within programming and it would work, but I would like a way to save those variables externally (in a notepad), so that "progress" is not lost. So my question is: how do I save a batch comma variable in a notepad that it creates? And how do I make the batch file read and separate the information placed in that binder?

For example, I set "% A%" to 2, "% B%" to 5 and "% C%" to 1. In notepad it saves "2 5 1" and so I use the batch again, it set on ABC in those same values.

    
asked by anonymous 14.11.2018 / 12:03

2 answers

0

You can save variables to files with the .bat extension.

In order to save the value of the variables in a file named VAR.bat it is necessary to clear the information from it if it exists with the command:

ECHO. > VAR.bat

Then:

ECHO SET A=%A% >> VAR.bat
ECHO SET B=%B% >> VAR.bat
ECHO SET C=%C% >> VAR.bat

To retrieve the value of the variable, just use the CALL command to call the VAR.bat file that already has the SET command that will reset the variables to the value that was saved in the file:

CALL VAR.bat
  

At the beginning of the program you will call the file and at the end save it.

    
14.11.2018 / 20:46
0

:: using only Notepad to save and retrieve A, B, and C values:

@echo off

set A=2
set B=5
set C=1

echo/%A%-%B%-%C%>.\abc.txt

:: lendo o bloco de notas (abc.txt) e recuperando os valores de A,B e C

for /f "tokens=1,2,3 delims=-" %%a in ('type .\abc.txt') do (

    set A=%%a
    set B=%%b
    set C=%%c
    echo/%A% %B% %C%
)

Using the Windows registry via setx and / or reg add / reg query / reg delete

    ::  escreva suas variáveis concatenando os valores numa chave do registro do windows
    ::  usando o comando setx ou reg add, de maneira que não seja necessário chamar outro
    ::  arquivo, nem saber o caminho dele. O setx registra variáveis para uso "global", 
    ::  porém só "seta" efetivamente este valor do próximo boot, daí entra o artifício de
    ::  ler os valores não numa variável, mas sim numa entrada de registro via reg query e 
    ::  apagando quando não mais for necessário.

        @echo off

        set A=2
        set B=5
        set C=1

        :: para escrever via setx: 
        setx a-b-c %A%-%B%-%C%

        :: ou ecreva direto no registro sem usar o setx via reg add:
        set a-b-c=%A%-%B%-%C%&
        reg add HKCU\Environment /v a-b-c /d "%A%-%B%-%C%" /f 2

        :: para ler o registro em outro tempo e recuperar os valores de %A%-%B%-%C%:
        for /f "tokens=3 delims=^ " %%i in ('reg query HKCU\Environment ^| findstr /i /c:"a-b-c"') do set a-b-c=%%i

        for /f "tokens=1,2,3 delims=-" %%a in ('echo/%%a-b-c%%') do (

            set A=%%a
            set B=%%b
            set C=%%c
            echo/%A% %B% %C%
        )

    :: ------- quando não for mais necessário usar a chave, você pode remover a entrada:
    reg delete HKCU\Environment /v a-b-c /f 2>nul >nul
    
18.11.2018 / 14:41