Search code examples
batch-filewindows-8

Batch - Read lines of one file except the ones starting with #


I would like to read a file line by line, but I would also like to ignore lines starting with #. I have tried to use this solution but it does not work for me. I am under Windows 8.

Batch file reading all the lines

@echo off
setlocal EnableDelayedExpansion
for /f "Tokens=* Delims=" %%x in (aliases.txt) do (
    set aliases=!aliases! "%%x"
)

aliases.txt

line 1 to read
# comment to ignore
line 2  to read

Solution

  • The eol character is the character that specifies the end of line comment character. Nothing on the line after this character will be read after it is. By default, it's ;, but you can set it to whatever you want.

    @echo off
    setlocal EnableDelayedExpansion
    for /f "eol=# Tokens=* Delims=" %%x in (aliases.txt) do (
         set aliases=!aliases! "%%x"
    )