Remove blank lines at the end of a text file

I have a txt file that gets uploaded to us on a nightly basis.  This file is processed by one of our systems.  On occasion this file has 2 or 3 blank lines at the very end.  This causes issues when being processed.  Need a way to remove these blank lines after it is uploaded before it gets processed.   I'd prefer some sort of dos batch file or script.  All lines have data except for the few at the end.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try if that works for you:
@echo off
set InFile=C:\Temp\test.txt
set OutFile=C:\Temp\test2.txt
(for /f "delims=" %%a in ('type "%InFile%"') do echo %%a)>"%OutFile%"

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve KnightIT ConsultancyCommented:
You can remove all blank lines using just

Find /v "" < file.txt > file2.txt

And then process file2.txt or rename as needed.

On phone so will pass more if needed when on PC.

The Find /v "" < file.txt > file2.txt doesn't work for me; file2 still has the empty lines.
What works is findstr.exe and a regular expression:
findstr.exe /r "." < test.txt > test2.txt

Open in new window

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Steve KnightIT ConsultancyCommented:
oBdA ... that is bizarre, agreed it doesn't work yet sure I've used it many times before!!
Bill PrewCommented:
I think you guys already knew this :).  See Q_26947689.

Looks like a reasonable short version is

findstr . file1.txt >file2.txt

Steve KnightIT ConsultancyCommented:
I suppose the other logical way is to select the lines that are wanted too, i.e. selecting all lines with comma or 0 or some other character that MUST be in each line for it to be imported anyway.

MohonkAuthor Commented:
Worked like a charm.  Thank you!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.