strip trailing CR/LF from text file.
Posted: 06 Oct 2016 03:55
quite simply is it possible and how am i able to remove a cr/lf code from a text file if it's the last entry in the text file?
A Forum all about DOS Batch
https://www.dostips.com/forum/
Code: Select all
@echo off
setlocal enableExtensions disableDelayedExpansion
set "inputFile=input.txt"
set "outputFile=output.txt"
:: test if file ends with a '\n' (true only if an additional "\r\n" will add no new line)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%inputFile%"') do set "n=%%a"
for /F "tokens=1* delims=:" %%a in ('^(type "%inputFile%" ^& echo^(^) ^| findstr /N "^" ') do set "m=%%a"
set /A "d=m-n"
:: 'copy' file
(
set "line="
if "%d%" == "1" for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%inputFile%"') do (
if not "%%~a" == "%n%" (
echo(%%b
) else (
set line=%%b
setlocal enableDelayedExpansion
<nul set /P "=!line!"
endlocal
)
) else (
type "%inputFile%"
)
) > "%outputFile%"
endlocal
goto :eof
amichaelglg wrote:thanks.
I only wanted to remove the very last code in the text file.
i.e remove any blank line at the end of the file.
Code: Select all
jrepl "(\r?\n)(?!\s\S)" "" /m /f yourFile.txt /o -
Code: Select all
jrepl "(^\r\n|^n)+(?!\s|\S)" "" /m /f yourFile.txt /o -
Code: Select all
jrepl "(\r?\n)+(?!\s|\S)" "" /m /f yourFile.txt /o -