1

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!,"
Anonymous
  • 13
  • 4
  • `endlocal` restores the environment to its condition when the matching `setlocal` was executed. Perhaps you should `echo` a message before each `setlocal` and `endlocal` to identify the flow you are creating. Would be a good idea to explain what the code is intended to do and edit your question to include example(s) of both source and result data (obfuscate as necessary) – Magoo Jul 20 '23 at 06:47
  • 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!," – Anonymous Jul 20 '23 at 06:54
  • The simple answer would be to remove all of your `setlocal` and `endlocal` commands within the `for` loop, and `echo %%i`. Obviously, this is not what you actually want tod do, as the simple method would then be `type filename`. – Magoo Jul 20 '23 at 07:00
  • I understand that I can echo %%i, but I need echo to work when enabledelayedexpansion, and echo %%i works only if disabledelayedexpansion (otherwise it doesn't treat ! as usual text) – Anonymous Jul 20 '23 at 07:08

1 Answers1

0

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?

jeb
  • 78,592
  • 17
  • 171
  • 225