Quick question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
renzlo
Posts: 116
Joined: 03 May 2011 19:06

Quick question

#1 Post by renzlo » 21 Jul 2011 03:09

guys, how do you remove an empty lines in a text file using dos batch?

I tried for loop then delimit the space, but i ended up with "echo on message" on the last part of my text file.

thanks in advance.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Quick question

#2 Post by dbenham » 21 Jul 2011 06:23

Well this is different - usually people want to know how to preserve blank lines.

Based on your description, you don't care about preserving leading spaces on your non-blank lines, and you are relying on FOR /F behavior of ignoring blank lines. You set delims = to a <space> in an attempt to convert a line containing only spaces into a blank line so that it is ignored.

I'm assuming your code looks something like this:

Code: Select all

for /f "%tokens=* delims= " %%L in (file.txt) do echo %%L

I would have guessed the code would work, but I just learned today that FOR /F ignores blank lines PRIOR to any delimiter parsing. So your lines containing nothing but space are preserved, but converted to a blank line :!:

The code is easily fixed by testing to see if %%L is empty:

Code: Select all

for /f "eol= tokens=* delims= " %%L in (file.txt) do if "%%L" neq "" echo %%L

I've also added eol=<space> so that lines beginning with ; are not skipped.

If your text file also has <tab> characters and you want to ignore lines containing nothing but <tab> or <space>, then you can simply use delims=<tab><space> instead of delims=<space>

Dave Benham

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: Quick question

#3 Post by renzlo » 21 Jul 2011 17:25

thanks for the reply Dave, but what if my text file has a line that have spaces in the start of the line that I want to preserve?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Quick question

#4 Post by dbenham » 21 Jul 2011 17:50

Then the solution is completely different.

you need to disable DELIMS and EOL (there are a few recent posts that show how to do this) I use my ForEntireLine macro.

then within the body of the FOR DO section:
- set "LN=%%a"
- setlocal enableDelayedExpansion
- set "TEST=!LN: =!" (eliminate all spaces)
- if defined TEST then echo !LN!
- endlocal

Dave Benham

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: Quick question

#5 Post by renzlo » 22 Jul 2011 02:14

thanks Dave for enlightening me. Thanks a lot.

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: Quick question

#6 Post by Acy Forsythe » 22 Jul 2011 07:29

you need to disable DELIMS and EOL (there are a few recent posts that show how to do this) I use my ForEntireLine macro.


So THAT is what that's for! You would think that with a name like "ForEntireLine" it would be obvious but sometimes you have to be beaten over the head with obvious before you see it!

I seriously thought that "ForEntireLine" was just a macro you used for the *entire FOR options line* with all of the line feeds and escapes necessary to make some of your macros work.

Now I have to go back and re-read all of that so it makes more sense now!

Someone needs to compile a batch rules book. I'm not going to suggest any names, but their initials are Dave and Jeb.

You can find little tips for using the parser's quirks here and there, usually with no explaination attached, just that it works. There are functions you can find everywhere, but one of the most complete sets is right here, but still even when those functions do use little tricks to make them work, it's not really explained.

The only place I've found yet that explains in any detail how and why things work and not just "this is what will do what you want to do, take it and run" is right here.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Quick question

#7 Post by alan_b » 22 Jul 2011 10:51

renzlo wrote:thanks for the reply Dave, but what if my text file has a line that have spaces in the start of the line that I want to preserve?

I think you are doing this all wrong.
You should use the "Code" button and use that to enclose the specific things you call "empty lines"
There are many types of empty lines. One man's empty line is another man's poison !

Code: Select all


                                                               



When I select the entire code box above, I can see one long blue line with lots of "space" characters
and above and below there are lines with no characters.

Regards
Alan

Post Reply