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!
I'm sure I've answered similar before...
Always verify against a test directory before applying to real data.
Note that if the filename does not contain separators like spaces, then both
usebackqand the quotes around%filename1%can be omitted.You would need to change the value assigned to
sourcedirto 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.txtcontaining 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
insectionis set to any value, tokenise the line read using{}as delimiters & assign the first token to%%b, the second to%%c(seefor /?from the prompt for docco, or thousands of examples on SO)if
%%ebegins[then either the section found is of interest (setinsectionto something) or it is not of interest (setinsectionto nothing)