读取批处理文件中的特定行

问题描述 投票:0回答:1

我正在尝试使用批处理脚本从 setup.ini 获取特定值的变量。

变量的名称是 ProductId,我注意到我有多个具有此名称的变量,但位于不同的部分。

这是我的(虚拟)setup.ini

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

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

这是我的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

在我的脚本中,我同时获得了

ProductId
,但我只需要获得
[install.msi]
部分 ->
{123456}
中的一个。

在网上我只能找到有关如何获取特定线路的解决方案,但也无法从特定部分找到解决方案。

我尝试了 chatGPT 的一些操作,但没有任何效果。

我正在考虑一个可能的解决方法:将

[install.msi]
部分复制到临时文件,然后阅读
ProductId
,我发现了类似这样的内容:

@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

但是这是我在控制台中看到的输出:

!line! 
!line! 
!line! 
!line! 
!line! 
!line! 
!line! 
!line! 
!line! 
batch-file ini
1个回答
0
投票

我确信我以前也回答过类似的问题...

@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

在应用于真实数据之前,始终验证测试目录。

请注意,如果文件名不包含空格等分隔符,则

usebackq
%filename1%
周围的引号都可以省略。

您需要更改分配给

sourcedir
的值以适合您的情况。该列表使用适合我的系统的设置。

我故意在名称中包含空格,以确保空格得到正确处理。

我使用了一个名为

q77542525.txt
的文件,其中包含您的测试数据。

使用字符串语法

set "var=value"
是 SO 的标准做法 分配,因为这可以确保忽略行上的杂散尾随空格。

首先,初始化变量。

将文件的每一行读取到

%%e

如果

insection
设置为任何值,则使用
{}
作为分隔符对读取的行进行标记,并将第一个标记分配给
%%b
,将第二个标记分配给
%%c
(请参阅 docco 提示中的
for /?
,或千SO 的例子)

如果

%%e
开始
[
,则找到的部分要么是感兴趣的(将
insection
设置为某些内容),要么不感兴趣(将
insection
设置为 无任何内容)

© www.soinside.com 2019 - 2024. All rights reserved.