【问题标题】:Set into a variable a specific line from a file in Windows batch将 Windows 批处理中文件中的特定行设置为变量
【发布时间】:2018-03-07 11:50:37
【问题描述】:

我试图从 Windows 批处理中的文件中获取一行,但没有成功。我已经尝试了几种方法来做到这一点,我不是专家,这对我来说是不可能的。要读取的数轴是动态计算的:

set "file=C:\myfile.txt"

REM Looking for line number where the segment "<segment>" is.
for /f "tokens=1 delims=:" %%L in ('findstr /n "<segment>" %file%') do ( 
    set begin_line=%%L
)

echo %begin_line%

所以我正在尝试读取 %begin_line%+1 行并将其存储到 var 中。 我确信实现这一点的方法是使用 for /f skip=%begin_line,但我做不到。

有什么想法吗?

我希望我的要求是可能的。

伊万

【问题讨论】:

    标签: windows batch-file echo


    【解决方案1】:

    我不确定我是否理解你的问题。您正在搜索包含子字符串 segment 的第一行。然后你想将 next 行存储在一个变量中。那是对的吗?如果是,代码如下:

    @ECHO OFF
    SETLOCAL EnableDelayedExpansion
    SET inputFile=C:\myfile.txt
    SET fount=0
    FOR /F "tokens=*" %%F IN (%inputFile%) DO (
        IF !found!==1 (
            SET foundLine=%%F
            GOTO BREAK
        )
        SET line=%%F
        SET line=!line:^<segment^>=!
        IF NOT !line!==%%F SET found=1
    )
    :BREAK
    ECHO !foundLine!
    PAUSE
    

    【讨论】:

      猜你喜欢
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      相关资源
      最近更新 更多