0

So I know how to save strings to a text file as a list from batch by using

set /p Myvar1="Enter your variable name/value: "
set /p Myvar2="Enter your variable name/value: "
set /p Myvar3="Enter your variable name/value: "

And then append to a text file

echo %Myvar1% %Myvar2% %Myvar3% >> Mylist.txt

So when I open the text file through the batch file, it can be displayed as a list:

SET "variables="
ECHO =================================================
ECHO               My list of stuff
ECHO =================================================
< Mylist.txt (
  set /p MyVar1=
  set /p MyVar2=
  set /p MyVar3=  
) 
::set line
ECHO -             [0] - %Myvar1%
ECHO -             [1] - %Myvar1%
ECHO -             [2] - %Myvar1%

Now the problem is that:

For each new line on the Mylist.txt text file I have to manually add lines on the batch file. On the provided example the batch is setup so it displays 3 lines of text from the text file. If the text file has 10 lines, it will only show the first 3 lines because that is what is specified. So I would like to accomplish the opposite of this script.

The batch file should be able to:

  • Batch file reads Mylist.txt.
  • For each line in Mylist.txt file the batch file creates a "numerated variable".
  • Each "numerated variable" can be addressable so the user can be prompted to select one of the options on the list 1, 2, 3, 4, etc.
Mofi
  • 46,139
  • 17
  • 80
  • 143
Wolf
  • 115
  • 3
  • 13
  • Related: [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/a/10167990). – aschipfl Aug 01 '17 at 09:20

2 Answers2

0

simple thing with a for loop plus a counter:

setlocal enabledelayedexpansion
set i=0
for /f "delims=" %%a in (mylist.txt) do (
  set /a i+=1
  set var[!i!]=%%a
)
echo number of variables: %i%:
set var[
echo ---------
for /l %%a in (1,1,%i%) do echo variable %%a = %var[%%a]%
Stephan
  • 53,940
  • 10
  • 58
  • 91
0

The easiest method to save variable names and their values to a file is redirecting the output of command SET to a file.

set My >"%APPDATA%\MyVariables.txt"

This simple line saves all environment variables starting with My in name with name and value into file MyVariables.txt in application data directory of current user.

And following command line can be used in a batch file to reload those environment variables from file:

for /F "usebackq delims=" %%I in ("%APPDATA%\MyVariables.txt") do set "%%I"

Same command line for usage directly in a command prompt window:

for /F "usebackq delims=" %I in ("%APPDATA%\MyVariables.txt") do set "%I"

A little bit more secure would be checking if each line in the file MyVariables.txt really starts with My as expected and so was not manipulated by someone who wants to override for example PATH by adding manually to file MyVariables.txt a line starting with PATH=.

It might be useful to know how many variables were loaded from file. This can be achieved with a simple extension:

set "VariablesCount=0"
for /F "usebackq delims=" %%I in ("%APPDATA%\MyVariables.txt") do set "%%I" & set /A VariablesCount+=1

Of course if there is an environment variable MyVariablesCount starting with My then this variable with its value would be also saved into the file and reloaded from file which would make it unnecessary to count the number of variables set from file.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • for /?
  • set /?

Read also the Microsoft article about Using Command Redirection Operators.

Mofi
  • 46,139
  • 17
  • 80
  • 143