Store diverse information in a batch notepad

Asked

Viewed 535 times

1

Good morning, first I’ll put it in context. Basically I’m making an "interactive story", where basically, during the progress of history I’ll have 3 variables. I could save these variables with the set command inside the programming and it would work, but I would like a way to save these variables externally (in a notepad), so "progress" is not lost. So my question is: how do I save variables from a batch coma in a notepad that it creates? And how do I make the batch file to read and separate the information placed in this created notepad?

For example, I set "%A%" as 2, "%B%" as 5, and "%C%" as 1. In the notepad it saves "2 5 1" and as soon as I use the batch again, it sets in A B C at those same values.

2 answers

0


  • Update: item 1) Saving in Bat Itself

@echo off & setlocal enabledelayedexpansion & title <nul & title Q344161

for /f "skip=8tokens=1,2,3delims=-" %%a in ('type "%~f0"')do if "%%a-"=="-" (
   goto :_salve_variaveis ) else echo/A = %%a B = %%b C = %%c & goto :eof

:_salve_variaveis
set /a A=1 + 1, B=4 + 1, C=0 + 1 & cmd /v /c echo/!A!-!B!-!C!->>"%~f0" & exit /b
rem :: As suas variaveies sarao salvas abaixo:

inserir a descrição da imagem aqui


  • The options I know and have used/tested:

    1) In Bat Itself

    2) In the abc.txt File

    3) In the Window Registry


1) :: Using the bat itself to save/retrieve variables on itself!! ::

@echo off & setlocal enabledelayedexpansion
for /f "skip=12 tokens=1,2,3 delims=-" %%a in ('type "%~f0"') do ( 
     if "%%a-" == "-" goto :_salve_variaveis
     echo/A = %%a B = %%b C = %%c && goto :eof
    )
:_salve_variaveis
set /a A=1 + 1
set /a B=4 + 1
set /a C=0 + 1
echo/!A!-!B!-!C!->>"%~f0"
call "%~f0" && exit /b
rem | As suas variáveis serão salvas na linha abaixo:

2) :: Using only notepad (abc.txt) to save and recover A, B and C values ::

@echo off

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

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

:: Reading the notepad (abc.txt) and recovering the values of A, B and 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%
)

3) :: 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ó efetivamente disponibiliza a variável na próxima vez que chamar o cmd, 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 

        :: 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%
        )

:: When you no longer need to use the key, you remove the input ::

reg delete HKCU\Environment /v a-b-c /f 2>nul >nul

0

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

To save the value of variables in a file named VAR.bat it is necessary to clear the information from it in case they exist with the command:

ECHO. > VAR.bat

Afterward:

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

To recover the value of the variable just use the command CALL to call the file VAR.bat who is already in command SET 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.