Question

Keeping Blank lines in a file

Asked by: johanzn

I am using the follwing batch file to read in a text file and then move all lines other than the line that has the page break in it over one space.  The file will sometimes have multiple blank lines at the end with a page break at the end.  How do I keep the blank lines in the new file?  It keeps the first 2 blank lines and then ends the batch file.  Any ideas?

Thanks
Chris


@echo off
if [%1]==[] goto usage_err
if not exist %1 goto usage_err

If exist output.dat del output.dat > nul
set filein=%1

for /f "delims==" %%f in (%filein%) do (
  set fileline=%%f
  call :process
  )

Echo Done!!!
set fileline=
goto exit

:process
echo "%fileline%" | findstr  >> Nul
if %Errorlevel%==0 goto found
if %Errorlevel%==1 goto Not_Found

goto :eof
:found
set zln=%fileline:~1%
if not defined zln goto blankline
@echo %fileline% >> output.dat
goto :eof

:Not_found
@echo  %fileline% >> output.dat
goto :eof

:blankline
echo. >> output.dat
goto :eof

:usage_err
echo usage: %0 filename

:exit

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-06-19 at 16:34:45ID20654356
Tags

blank

,

errorlevel

,

findstr

Topic

MS DOS

Participating Experts
3
Points
50
Comments
7

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. DOS batch errorlevel
    The following code, in a DOS batch file, works on NT. However, it does not work on Windows 98. It always prints "file is empty." How to make this work or how do I judge if a file is empty on Windows 98? find /v "" file if errorlevel=1 echo file is empty. ...
  2. NT4 DOS Batch: Cant get echo pipe findstr to work
    I have a batchfile that I've modified from an earlier question. The batch does an FTP then checks for 5## error codes in the FTP output, this is great unless there's a file that happens to be 5###.. bytes in size as this is also caught as a error: 525 File Not Found 5000 by...
  3. DOS errorlevel
    can some one tell me what each errorlevel # mean. You read one of our batch file having these lines like if not errorlevel 1, if not errorlevel 2,... to 9.
  4. batch errorlevel problem
    Hello all, I'm pretty new to EE so please move this topic if I placed it in the wrong place. Now my problem: I'm making a script to check several servers on mail relay. I'm using batch with Blat. My script: code === for /F "delims=@" %%d in (to.ini) do set MT=...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: K_2KPosted on 2003-06-19 at 20:19:23ID: 8763314

Hi johanzn,

a few ideas, several questions:

First of all, I'm not feeling the purpose of the findstr in
   :process
   echo "%fileline%" | findstr  >> Nul
   if %Errorlevel%==0 goto found
   if %Errorlevel%==1 goto Not_Found

My XP gives me nothing but command line error on the findstr line.  This one:
   echo "%fileline%" | findstr "." >Nul

Always passes, but doesn't seem to notice the difference between a space, blank line, and a Form Feed.  That is, :Not_Found never runs.  Is your Page Break something other than 0xC (FF)?  Can you search specifically for <Your Page Break> && goto :Not_found  (maybe change it's name to :PageBreak?)


Also,  it seems you're using zln to correctly represent all but the first column of all normal lines, yet echoing the original lines back to output.dat.  maybe
     @echo %fileline% >> output.dat
could become
     @echo %zln% >> output.dat
except i had some lines in my file that had spaces in it but nothing else.  That caused the line to change to:
     ECHO is off.
Using another FOR /F . . .  to find if there is not a first word may be a better way to choose :blankline.


Let us know what you come up with, or tell us what your page break looks like so maybe we can help choose the loop to call from within the FOR /F and avoid some of the other problems altogether.


Good Luck,

2K
(\o/)


 

by: K_2KPosted on 2003-06-19 at 23:16:22ID: 8764144

hmmm,  took a bit of a relook, then i got mad at the interpreter for misinterpreting me and had a long argument with this box.

We aggreed quickly that you got NONE of your blank lines reproduced.  FOR by design will skip all blank lines as per "FOR /?"  The 2 blank lines you see may have nonprintable characters or multiple spaces, or were non blank lines modified to become blank somewhere in your process.

We also aggreed quickly that you can filter your lines without a FINDSTR command.

It took a very little coaxing for me to convince this box there should be a way to number the lines in a filterable way so FOR doesn't skip them.  I ended back up with FINDSTR, but this time telling the PC NOT to use it as a filter, but as a line modifier, letting FOR filter out what I added.

The bigger argument was over symantecs of which of the three basic questions to ask first:
   Is line blank?
   Is line Special (page break?)
   Is line normal?

And this thing about lost the right to exist when it complained out of the blue about some Drive not being found when it should have said it can't handle two adjacent null label lines inside an IF clause, but that's another thread.


Anyway, we came up with the thing below that preserves blank lines and odd lines and removes the first column only from lines that begin with space.  Caviat:  Lines with 2 or more spaces and nothing else are reduced to blank lines.

If your file start each collumn with something else, then you can adjust the bad character.
If you may have a few different characters there, you must add if clauses for each,  OR fix the special case to some first and assume all else can be chopped.

- Enjoy,

2K
(\o/)


:: 2K_Does_Delete_Column_One_From_Text_File.cmd
@echo off
if "%1"=="" goto usage_err
if not exist %1 goto usage_err

If exist output.dat del output.dat > nul
set filein=%1
:: first char after quote is the one lines MUST start with to be shifted
set BadCharNoBiscuit=" "
set BadCharNoBiscuit=%BadCharNoBiscuit:~1,1%
:: that could have been done in one line, but wouldn't look readable


:: For skips blank lines
:: line numbers from FINDSTR makes all lines non-blank
for /f "delims=: tokens=1,*" %%j in ('findstr /N "^.*$" %filein%') do (
 set fileline=%%k
 call :process
)

Echo Done!!!
set fileline=
set zln=
goto exit

:process
:: first word K prevents blank line problem in FOR loop change 1 here to the number of columns to nuke but only the first is checked.
set zln=K %fileline:~1%
:: What type of line is it?
if "%fileline%"=="" (
  :: Blank line
  echo. >>output.dat
) else if "%fileline:~0,1%"=="%BadCharNoBiscuit%" (
  :: read tokens to see if there is a word in the line
  for /f "tokens=1,*" %%j in ('echo %zln%') do (
    if "%%k"=="" (
      :: no first word found
      echo. >>output.dat
    ) else (
      :: echo will work but use line, not tokens
      echo %zln:~2% >>output.dat
    )
  )
) else (
  :: assume page break or all text needed if not space
  echo  no space at start of ^|%fileline%
  echo %fileline% >>output.dat
)
goto EOF


:usage_err
echo usage: %0 filename

:exit
:EOF

 

 

by: K_2KPosted on 2003-06-19 at 23:51:28ID: 8764293

Note to self:  Never add "BadCharNoBiscuit" after starting a post nor move if clauses inside a post.  

Here's the tested copy after corrections:

Really, this time i didn't MOD it after the fact, tho i did simplify the IF clauses before testing.

2K
(\o/)


:: 2K_Does_Delete_Column_One_From_Text_File.cmd
@echo off
if "%1"=="" goto usage_err
if not exist %1 goto usage_err

If exist output.dat del output.dat > nul
set filein=%1
:: first char after quote is the one lines MUST start with to be shifted
set BadCharNoBiscuit=" "
set BadCharNoBiscuit=%BadCharNoBiscuit:~1,1%
:: that could have been done in one line, but wouldn't look readable


:: For skips blank lines
:: line numbers from FINDSTR makes all lines non-blank
for /f "delims=: tokens=1,*" %%j in ('findstr /N "^.*$" %filein%') do (
set fileline=%%k
call :process
)

Echo Done!!!
set fileline=
set zln=
goto exit

:process
:: first word K prevents blank line problem in FOR loop.  Change 1 here to the number of columns to nuke but only the first is checked.
set zln=K %fileline:~1%
:: What type of line is it?
if "%fileline%"=="" (
  :: Blank line
  echo. >>output.dat
  goto EOF
)
if "%fileline:~0,1%"=="%BadCharNoBiscuit%" (
  :: read tokens to see if there is a word in the line
  for /f "tokens=1,*" %%j in ('echo %zln%') do (
    if "%%k"=="" (
      :: no first word found. We lose the useless spaces here
      echo. >>output.dat
    ) else (
      :: echo will work but use line not tokens
      echo %zln:~2% >>output.dat
    )
  )
  goto EOF
)
:: assume page break or all text needed if not space or blank
echo %fileline% >>output.dat
goto EOF


:usage_err
echo usage: %0 filename

:exit
:EOF

 

by: SteveGTRPosted on 2003-06-20 at 08:11:29ID: 8767144

Check out Bill's solution in this question:

http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_20642198.html

It's really very good. It uses Find to number the file, the processes the file, then strips the numbers off the front.

Good Luck,
Steve

 

by: K_2KPosted on 2003-06-20 at 09:42:53ID: 8767924

Steve & Bill,

Sry I didn't do a search for find /n and findstr /n to get that answer.

Other than minor symantics and preferences of simpler to read using more variables and a temp file over my cryptologicalation (we can make up words can't we?) with minor documentation, it looks like the same thing indeed.  Minor changes in the what to print "IF" statements and what to cut ~1 or ~1 would make it feasible for this question.

Also notable: Bill's solution works on more OS versions than mine, as some do not do grouping in IF statements and he uses calls intead.  Maybe someday I'll get on the bandwagon.


Good Job.

2K
(\o/)

 

by: billiousPosted on 2003-06-23 at 09:43:27ID: 8782811

Is this under control & done, or are there difficulties?

K_2K : Your RA bounces.

...Bill

 

by: K_2KPosted on 2003-06-23 at 13:54:29ID: 8784608

Johanzn,
Yes,  let us know if you need more help.


Bill,
Hmmm.  
Have had problems entering such a short address on some web pages that think "2K<at>pa.net" is not valid, but i didn't think any real mail programs out there had a problem with it.  I'll try to track down where i sent it from and see if i have a messed up client sending the wrong RA.  Also added to profile, not that i see EE is not showing it ( and right they shouldn't ).  

Thanks,

2K
(\o/)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...