Batch: delete line feed from end of text file?

我只是一个虾纸丫 提交于 2019-12-03 10:00:54

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

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 ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!