1

Need help in creating a batch file which reads a variable value from *.txt file.

Eg.,

MyTxtFile.txt
VAR_A=5
VAR_B=10

MyBatFile.bat Needs to read MyTxtFile.txt and echo the value of VAR_A and VAR_B

Sujith Shiva
  • 31
  • 1
  • 3
  • 1
    Possible duplicate of [Open and write data to text file using bash/shell scripting](https://stackoverflow.com/questions/11162406/open-and-write-data-to-text-file-using-bash-shell-scripting) – Morteza Jalambadani Sep 27 '18 at 09:49
  • Thanks for the link. But I dont want to write anything to the .txt file. I need to print the value of .txt -> VAR_A using a batch file. – Sujith Shiva Sep 27 '18 at 10:03
  • 1
    What have you tried, where are you stuck? Please provide a [mcve] of your code! – aschipfl Sep 27 '18 at 10:43
  • I have not tried anything as I am not so used to the batch file scripting. I dont know if this can be achieved through the batch file. – Sujith Shiva Sep 27 '18 at 11:01
  • 4
    `for /F "delims=" %%a in (MyTextFile.txt) do set "%%a"` – Aacini Sep 27 '18 at 12:22
  • Well I don't know of too many programming languages these days that cannot read a file. So maybe you could have started by researching that topic and attempting to code it. Ignorance or lack of experience is not an excuse for not putting in some effort. – Squashman Sep 27 '18 at 12:38
  • @Aacini, you might as well post that as an answer. It is better then the two below IMHO. – Squashman Sep 27 '18 at 13:54

2 Answers2

2

Depends on how these values have been written. If you can add small "tags" before the values then it makes this job easy and done.

For example, here is a text file I made:

random random
VAR-A 2853
VAR-B 1039410
our code wont notice this text

And the batch file ran on it and outputted this:

2853
1039410

Which in the code is written as:

echo %varA%
echo %varB%

Full code is here:

@echo off
cls
cd %~dp0

rem Extracting lines with the tag "VAR-A/B"...

findstr "VAR-A" myfile.txt > Vara.txt
findstr "VAR-B" myfile.txt > Varb.txt

rem Setting the lines to variables...

set /P varA=<Vara.txt
set /P varB=<Varb.txt

rem Deleting temp files...

del Vara.txt
del Varb.txt

rem Now the 2 variables have tags before them, time to remove them...

set varA=%varA:~6%
set varB=%varB:~6%

rem Output:

echo %varA%
echo %varB%

pause>nul
  • Wow, thanks. This works. Is there a way to extract without the tags as the length of the variable name is not fixed. – Sujith Shiva Sep 27 '18 at 10:19
  • 1
    [This guy](https://stackoverflow.com/a/5841587/8458095) showed an easy way to measure string length. Try it out. –  Sep 27 '18 at 10:35
1

You could try the following (see the explanatory rem comments to learn how it works):

@echo off
rem // Define constants here:
set "_FILE=MyTxtFile.txt"
rem // Determine number of lines in text file:
for /F %%C in ('^< "%_FILE%" find /C /V ""') do set "COUNT=%%C"
rem // Read from text file:
< "%_FILE%" (
    rem // Loop over number of lines:
    for /L %%I in (1,1,%COUNT%) do (
        rem // Clear line variable:
        set "LINE="
        rem // Read current line into variable:
        set /P LINE=""
        rem // Check whether current line is not empty:
        if defined LINE (
            rem // Apply current line as variable assignment (avoid messages for invalid ones):
            > nul 2>&1 call set "%%LINE%%"
        )
    )
)

Run the batch file (let us call it assign.bat) from a command prompt window. To show the assigned variables, type set VAR_ into the prompt:

>>> assign.bat

>>> set VAR_
VAR_A=5
VAR_B=10
aschipfl
  • 33,626
  • 12
  • 54
  • 99