Solved Delete Last Empty Line of text file

September 19, 2012 at 11:37:28
Specs: Windows XP
I have text file with 1 or 2 empty line on the last. I need a batch script to delete the last (1 or 2) empty line
for example :
empty line
empty line

After run the script, it should be
the last empty line will be removed.
Please advice. Thanks a lot

See More: Delete Last Empty Line of text file

Report •

September 19, 2012 at 16:46:42
✔ Best Answer
this might serve IF there are no other empty lines you want to keep:

@echo off
for /f "tokens=*" %%a in (textfil) do >>noblanks echo %%a
move /y noblanks textfil
::===== end

be sure to keep a backup copy of the original file, since that last command writes over it.

Report •

September 27, 2012 at 02:51:05
Hi nbrane,
It will create "Echo is off" in the last line.
Please info how to remove "Echo is off".

Report •

September 27, 2012 at 17:37:28
I could not duplicate the error on my xp, so I'm not sure. maybe there's something in one of those last lines... but you could try this:

for /f "tokens=*" %%a in (textfil) do >>noblanks echo.%%a

although I doubt that will resolve the issue. You might need to PM your file (or a close facsimile) to me so I can try it. I tried it with both a space, and a null (char. zero), and still didn't get the junk. worst case, try this:

for /f "tokens=*" %%a in (textfil) do if "%%a" neq "" >>noblanks echo.%%a

Report •

Related Solutions

Ask Question