2

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?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Peter
  • 1,786
  • 4
  • 21
  • 40
  • possible duplicate of [How to delete Linefeed using batch file](http://stackoverflow.com/questions/3116022/how-to-delete-linefeed-using-batch-file) – CharlesB Jun 06 '11 at 15:46
  • 1
    why doesn't the linked question work? – CharlesB Jun 06 '11 at 15:46
  • @CharlesB: I honestly have no idea. When I run `for /f "delims= tokens=*" %%x in (inputfile.txt) do echo %%x >> outputfile.txt` it gives me the same output - the last line feed is still there. Is there any other way to solve that issue? – Peter Jun 07 '11 at 06:40

2 Answers2

3

Try the following code as an starting point

@echo off
copy %1 temp.txt
echo d >debug.tmp
echo r >>debug.tmp
echo a >>debug.tmp
echo dec cx >>debug.tmp
echo dec cx >>debug.tmp
echo. >>debug.tmp
echo g =100 102 >>debug.tmp
echo w >>debug.tmp
echo q >>debug.tmp
debug temp.txt <debug.tmp

This batch, first, copies the file to a temporary file that needs to have a 8.3 name.

Then it prepares a debug script to chop off the last two bytes of the temporary file.

The first two debug instructions R and D are only to show the contents of the file and the register (with the important CX value that contains the length of the file) They can be removed.

Then the debug script enters assembler mode A and produces two DEC CX instructions, that decrement twice the value of CX. The blank line leaves assembler mode.

The script executes G the two assembly instructions.

Then the debug script writes W back to the file the same contents read, minus two bytes in length. And finally quits Q debug.

This code only works with files smaller than 64kB. For bigger files you need to extend the assembly code, checking for carry flag after decrementing CX to decrement BX.

For more information read DEBUG /? and then try DEBUG and ?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
PA.
  • 28,486
  • 9
  • 71
  • 95
  • +1, Nice idea to use debug for this, but in this case I would prefer jScript/vbscript as it should also work on win7/64bit – jeb Jun 07 '11 at 08:56
3

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

jeb
  • 78,592
  • 17
  • 171
  • 225
  • Hello Jeb, thank you - this works fine but it also deletes the `"` which I have at the very beginning and at the end of each line. Do you know how I could fix that? Thank you again, Peter – Peter Jun 07 '11 at 12:39
  • In your case you could change the `set /p` line to ` – jeb Jun 07 '11 at 21:04
  • Hello Jeb, one more question: the source file I need to append to `>> out.txt` has 1 line break at the end. When I use your batch that line break gets deleted - perfect. But how could I preserve that one line break (just not add another one). Or should I open a new question for that? Thank you very much again, Peter – Peter Jun 09 '11 at 10:45
  • At best you open a new question and post the content of sample files – jeb Jun 09 '11 at 13:00
  • Excellent answer. It worked for me tweaking the code a little bit. I added my edit, as it was missing a ) and the > out.txt was on the outside the loop. thanks – Jiraheta Nov 17 '14 at 23:25
  • @Jiraheta Tanks, but I rolled your edit back, it looks strange but the code was correct. The parenthesis in `(echo()` are balanced. And the redirect outside of the loop was also intention – jeb Nov 18 '14 at 21:43
  • @jeb How do I make this work with file paths? I need to point the source file as `D:\AgrCC\AgrTest\Report Results\%1` and the destination file as `D:\AgrCC\AgrTest\Data Export\%1` but I end up with the source file path as content in the destination file.... – dadde Feb 01 '17 at 14:47
  • @jeb ok: [my new question](http://stackoverflow.com/questions/41983065/batch-delete-line-feed-from-end-of-text-file-using-filepaths) – dadde Feb 01 '17 at 15:09