How to read a text file and including empty lines in batch?

"FOR" in batch ignores empty lines, but you can use for-loop with findstr.

Here is an example that read a text file and include empty lines:


    @echo off & setlocal enabledelayedexpansion
    rem ; ----------set your file name/path ----------
    set "inputFile=in.txt"
    set "outFile=out.txt"
    rem ; ---------- call function ----------
    call :readFile "%inputFile%" "%outFile%"
    Type out.txt
    pause
    exit /B %errorlevel%
    
    rem ; ---------- function code ----------
    :readFile
        setlocal & rem most times enabledelayedexpansion
        cd.>"%~2" & rem create/write empty to %inputFile%
        rem write to %outFile% line by line, but "FOR /F" ignores empty lines, 
        rem so use findstr to prefix each line with a line number,
        rem then "!%%~i!" can get line data without the line number
        rem (don't need use 'set "line=!line:*:=!"' to remove it)
        >"%~2" (
            for /F "usebackq  tokens=1* delims=" %%i in (`findstr /n ".*" "%~1"`) do (
                if "!%%~i!" neq "" (echo !%%~i!) else echo.
            )
        )
    endlocal
    exit /B 0