Search code examples
batch-filecarriage-returnlinefeed

Batch: delete line feed from end of text file?


I have a .txt file where I need to get rid of the last line feed. Looking at the file in a HEX Editor it shows "0d 0a" at the end.

I have looked at the thread How to delete Linefeed using batch file but that did not help. I have tried COPY source target /b which also does not help.

Unfortunately I can't use Java or any third party tools, I need to use a batch file.

How can I get rid of that line feed at the end?


Solution

  • Using batch this should work.

    @echo off
    setlocal DisableDelayedExpansion
    set "firstLineReady="
    (
        for /F "eol=$ delims=" %%a in (myFile.txt) DO (
            if defined firstLineReady (echo()
            set "firstLineReady=1"
            <nul set /p "=%%a"
        )
    ) > out.txt
    

    It copies all lines and append to each line a CR/LF, but not to the last one