[BAT] I want to delete all "blank lines" in a text file!A script example that deletes all "blank lines" by dragging and dropping into a batch!


Hello!This time, I will show you how to create a batch file that erases all "blank lines" in a text file in an instant!

When you want to delete "blank lines", it is difficult to open a text file and delete it manually, isn't it?
It's not a big deal if it's just a few lines, but it's usually a lot in situations where you want to delete it.

At first glance, it seems easy to just delete the blank lines, but when you start thinking about the script, some people have a hard time saying, "What should I do? I don't know what to do, so I'll delete it manually ..." think.

This time, I would like to introduce an example of creating such a "batch script that erases blank lines"!

Thing you want to do

● Delete all "blank lines" by simply "drag and drop" the "text file" you want to delete into the "batch file".

* Text file = An ordinary text file with a .txt or .log extension.Please note that it is different from Word and Excel files.

Script example

@echo off
cd /d %~dp0

findstr /R . %1>>%~n1_Result%~x1

How to use

1. Paste the code copied in "Copy code!" To the newly created text file.

2.Save with the extension ".bat"

3. Drag and drop the text file for which you want to delete "blank lines" to the "~ .bat" file created in "2."

Then, a file with all "blank lines" deleted will be created in the same folder as the folder containing the text file.


The main this time is "findstrThe command is to "handle regular expressions".

Eventually,"From the drag-and-drop text file, just extract the line that contains the regular expression that represents one character, because anything is fine.It becomes a command.

The details of commands and terms are explained below.

What is the regular expression "."

For example, when you want to find out if there is a "three-digit number" in a sentence, if it is a normal conditional branch, "3 or 000 or 001 or 002 or 003 or ... 004" is in the sentence. It is necessary to check 999 times at the longest, "Does it exist?"

this,Collecting 3 patterns that are "three-digit numbers", "\ d {3}"Or"[0-9] {3}"Regular expression" can be searched only once with the special expression "".

This time I'm using a "regular expression" called ".""." Is "anything is fine, so just one character"Represents

I would like to explain regular expressions in detail in another article.

Find characters with Findstr

The "findstr" command isLines containing the characters you have decidedThis is a command to search for.
In other words,I don't find anything other than letters. for that reason,No characters on blank linesSo it is ignored.

findstr / R.% 1

"Findstr" = "character" or "regular expression" that you have decidedExtract the line of text that contains what you want to look up from the "text file".

findstr /R .% 1

"/ R" = "findstr" command option."Regular expression" instead of "character"It is an option to search for "text file" with.

findstr / R . %1

"." = As written above"Regular expression" which means "anything is fine, just one character".

findstr / R. %1

"% 1" = This is the first additional setting (called the first argument) of the executed batch file. When you drag and drop the file to the ".bat" file, it becomes this "first additional setting (first argument)", so here "Drag and drop text file".

That is, "findstr / R.% 1"Is"From the drag-and-drop text file (% 1), extract only the lines that contain the regular expression (/ R) that represents one character (.) Anyway (findstr).It becomes a command.

Save the completed file with "_Result" in the original name

This is the last part to save.Characters are added to the original file name that was dragged and dropped.

findstr / R.% 1>>% ~ n1_Result% ~ x1

">>"Is a redirect symbol,">>The execution result is added to the file specified at the end of ".In this case, "% ~ n1_Result% ~ x1I will add the result of findstr to the file.

"% ~ N1" indicates the original file name part that was dragged and dropped, and "% ~ x1" indicates the original extension.

[Original file name] _Result [Original extension]

In the form of, it is automatically determined and saved in the batch.
* Because it is in the form of additional writing, if it is already saved with the same file name, it will be added after that, so please be careful.

Also, this article also explains "% ~", so please refer to it.


We have introduced a method to remove "blank lines" from a text file.

It's not enough to add new software because it only removes blank lines, but if you can do it manually, you can't say anything you don't want to do ...

Originally, I would like to create it while formatting the text in advance so that such work does not occur from the beginning, or if it is a program or system, adjust the output file. (However, I noticed that the text was full of blanks, but ^^;

If you happen to want to get rid of "blank lines" quickly, please try using the batch created this time!

I hope it helps you.


Translate »
I copied the title and URL