Reading specific line in batch file

53 Views Asked by At

I'm trying to get a specific value's variable from a setup.ini with a batch script.

The variable's name is ProductId and I noticed that I have multiple variables with this name, but in different section.

This is my (dummy) setup.ini:

[install.msi]
key1 = value1
key2 = value2
ProductId={123456}
key3 = value3

[mkl64.msi]
keyA = valueA
keyB = valueB
keyC = valueC   
ProductId={ABCDEF}

This is my script.bat:

@echo off

REM set consol page on UTF-8
chcp 65001 > nul

REM setup.ini directory (my file is in the same folder )
set "file=setup.ini"

REM variable to look for
set "variable=ProductId"

REM look for specific line in setup.ini
for /f "tokens=2 delims==" %%i in ('type "%file%" ^| findstr /i "%variable%"') do (
    set "value=%%i"
    setlocal enabledelayedexpansion
    echo Il valore cercato è: !value!
    endlocal
)

chcp > nul

pause

With my script I got both ProductId, but I need to get only the one in the [install.msi] section -> {123456}.

Online I only find solutions on how to get a specific line, but not from a specific section also.

I tried something from chatGPT but nothing is working.

I was thinking about a possibile workaround: copy the [install.msi] section to a temporary file and then read the ProductId and I found something like this:

@echo off

REM set consol page on UTF-8
chcp 65001 > nul

REM setup.ini directory (my file is in the same folder )
set "file=setup.ini"

REM section to look for
set "section=[install.msi]"

REM file temp name
set "fileTemp=temp.ini"

REM create or overwrite file temp
echo. > "%fileTemp%"

REM flag to see if you are in the right section
set "sezioneTrovata="

REM look for the right section and copy in the temp file
for /f "tokens=*" %%a in ('type "%file%" ^| findstr /n /r "%section%"') do (
    set "line=%%a"
    set "line=!line:*:=!"
    
    REM check if section is found
    if defined sectionFound (
        REM if you are in the right section copy
        echo !line! >> "%fileTemp%"
    ) 
)
chcp > nul

pause

But this is the output I see in the console:

!line! 
!line! 
!line! 
!line! 
!line! 
!line! 
!line! 
!line! 
!line! 
2

There are 2 best solutions below

0
Magoo On

I'm sure I've answered similar before...

@ECHO OFF
SETLOCAL
rem The following settings for the directory and filename are names
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q77542525.txt"

SET "productid="
SET "insection="

FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO (
 IF DEFINED insection FOR /f "tokens=1,2 delims={}" %%b IN ("%%e") DO IF "%%b"=="ProductId=" SET "productid=%%c"
 ECHO %%e|FINDSTR /b "[" >NUL
 IF NOT ERRORLEVEL 1 IF "%%e"=="[install.msi]" (SET "insection=Y") ELSE (SET "insection=")
)

ECHO Product ID is %productid%

GOTO :EOF

Always verify against a test directory before applying to real data.

Note that if the filename does not contain separators like spaces, then both usebackq and the quotes around %filename1% can be omitted.

You would need to change the value assigned to sourcedir to suit your circumstances. The listing uses a setting that suits my system.

I deliberately include spaces in names to ensure that the spaces are processed correctly.

I used a file named q77542525.txt containing your data for my testing.

It is standard practice on SO to use the syntax set "var=value" for string assignments as this ensures stray trailing spaces on the line are ignored.

First, initialise variables.

Read each line of the file to %%e.

If insection is set to any value, tokenise the line read using {} as delimiters & assign the first token to %%b, the second to %%c (see for /? from the prompt for docco, or thousands of examples on SO)

if %%e begins [ then either the section found is of interest (set insection to something) or it is not of interest (set insection to nothing)

0
Aacini On

This is a simple solution:

@echo off
setlocal EnableDelayedExpansion

set "searchSection=[install.msi]"

set "section="
for /F "tokens=1,2 delims== " %%a in (test.txt) do (
   if "%%b" equ "" set "section=%%a"
   if "!section!" equ "%searchSection%" set "%%a=%%b"
)

echo ProductId = %ProductId%
echo key3 = %key3%

Output:

ProductId = {123456}
key3 = value3

Note that this code extracts all variables in the search section, like key3 in the example...