DOS script to delete empty lines inside the file

Posted on 2006-03-29
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
    LVL 2

    Expert Comment

    @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


    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
    LVL 2

    Accepted Solution

    @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

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT ( The crux of this batch f…
    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…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now