Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


DOS script to delete empty lines inside the file

Posted on 2006-03-29
Medium Priority
Last Modified: 2010-07-27
I want to create DOS batch file to loop through file (text files) and delete all empty lines.
Question by:calju
  • 2

Expert Comment

ID: 16330175
@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.

Author Comment

ID: 16330205

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

Accepted Solution

JoshdanG earned 1000 total points
ID: 16330337
@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

echo Error: backup.txt exists, unable to create working file
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 :-) ).

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question