DOS script to delete empty lines inside the file

I want to create DOS batch file to loop through file (text files) and delete all empty lines.
caljuAsked:
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.

JoshdanGCommented:
@echo off
type nul > output.txt
for /f "delims=" %%i in (%1) do echo %%i>> output.txt

--------------------------
Save as your_name.bat

usage: your_name file_to_parse.txt

Outputs all non-empty lines in file_to_parse to output.txt

Note that it intentionally wipes out output.txt before writing (just remove the second line to make it always append), and it also puts an endline on the last line even if you did not have one before.  Please let me know if either of these are an issue.
0
caljuAuthor Commented:
Thanx,

But if I want to sweep same file not output to output.txt? So I can use syntax like clean.bat and filename to clean is specified inside clean.bat
0
JoshdanGCommented:
@echo off
IF EXIST backup.txt goto BAILOUT
copy %1 backup.txt > nul
type nul > %1
for /f "delims=" %%i in (backup.txt) do echo %%i>> %1
del backup.txt
goto end

:BAILOUT
echo Error: backup.txt exists, unable to create working file
:end
-------------------
Okay, this one will now replace the original file with the new file lacking empty lines.  It uses a file named "backup.txt" and then deletes it when it is done.  It will check to make sure it doesn't copy over an existing "backup.txt"

You said that you wanted to specify the filename inside the file.  Right now if you saved the program as clean.bat, you could run it as:

    clean myfile.txt

and it would remove the empty lines from myfile.txt.  In windows, you can just drag the text file onto clean.bat and it will likewise strip the empty lines.  This seems more convenient to me, but if you really want to specify it inside the file, I can set that up too (though not tonight :-) ).
0

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
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.