Batch script, how to read exclamation marks in a text file like usual text and save the lines into variables?

huangapple go评论56阅读模式
英文:

Batch script, how to read exclamation marks in a text file like usual text and save the lines into variables?

问题

我需要在批处理脚本中读取一个txt文件,将每一行保存到变量中,并在启用延迟扩展时使用它。但是文本文件可能包含感叹号,它们应该被视为普通文本。我尝试了这段代码:

@echo off
setlocal
set file1=C:\BatchTest\TEXT.txt
set "WasFirstLineRead1="
setlocal disabledelayedexpansion
for /f "delims=" %%i in (%file1%) do (
    set "line=%%i"
    if defined WasFirstLineRead1 (
        endlocal
    )
    setlocal enabledelayedexpansion
    echo !line!
    set "line="
    endlocal
    setlocal disabledelayedexpansion
    set "WasFirstLineRead1=YES"
)
endlocal
pause

但它不会在解析第一行后更新变量line的值,它始终包含第一行。如何修复它?

例如,txt文件包含以下文本:

,"H!e!l!l!o! world!",
,"H!e!l!l!o! "H!e!l!l!o!
world!", world!",
world!", "H!e!l!l!o!

输出应该是相同的,但实际输出是:

,"H!e!l!l!o! world!",
,"H!e!l!l!o! world!",
,"H!e!l!l!o! world!",
,"H!e!l!l!o! world!",
英文:

I need to read a txt-file in batch script, save every line to variable and use it when delayed expansion is enabled. But the text file can contain exclamation marks, and they should be treated as usual text. I tried this code:

@echo off
setlocal
set file1=C:\BatchTest\TEXT.txt
set "WasFirstLineRead1="
setlocal disabledelayedexpansion
for /f "delims=" %%i in (%file1%) do (
    set "line=%%i"
    if defined WasFirstLineRead1 (
        endlocal
    )
    setlocal enabledelayedexpansion
    echo !line!
    set "line="
    endlocal
    setlocal disabledelayedexpansion
    set "WasFirstLineRead1=YES"
)
endlocal
pause

But it doesn't update the value of the variable line after parsing the first line, it always contains the first line. How can it be fixed?

For example, the txt-file contains this text:

,"H!e!l!l!o! world!,"
,"H!e!l!l!o! "H!e!l!l!o!
world!," world!,"
world!," "H!e!l!l!o!

The output should be the same, but the actual output is

,"H!e!l!l!o! world!,"
,"H!e!l!l!o! world!,"
,"H!e!l!l!o! world!,"
,"H!e!l!l!o! world!,"

答案1

得分: 0

我不明白为什么你要用 WasFirstLineRead1 构建这样一个复杂的解决方案。

你只需要切换延迟扩展。

@echo off
setlocal
set file1=C:\BatchTest\TEXT.txt

setlocal disabledelayedexpansion
for /f "delims=" %%i in (%file1%) do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    echo(!line!
    endlocal
)

如果你想处理空行或以“;;”开头的行,你应该切换到:

@echo off
setlocal
set file1=C:\BatchTest\TEXT.txt
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%L in (`"findstr /n ^^ %file%"`) do (
    set "line=%%L"
    SETLOCAL EnableDelayedExpansion
    set "line=!line:*:=!"
    echo(!line!
    ENDLOCAL
)

有关解释,请参阅批处理文件:如何读取文件?

英文:

I don't see why you build such a complex solution with WasFirstLineRead1

You only need to toggle delayed expansion.

@echo off
setlocal
set file1=C:\BatchTest\TEXT.txt

setlocal disabledelayedexpansion
for /f "delims=" %%i in (%file1%) do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    echo(!line!
    endlocal
)

If you want handle empty lines or lines beginning with ";" you should switch to

@echo off
setlocal
set file1=C:\BatchTest\TEXT.txt
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%L in (`"findstr /n ^^ %file%"`) do (
    set "line=%%L"
    SETLOCAL EnableDelayedExpansion
    set "line=!line:*:=!"
    echo(!line!
    ENDLOCAL
)

For explanations see Batch File: How to read a file?

huangapple
  • 本文由 发表于 2023年7月20日 14:09:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定