Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I include blank lines in a for /F statement

Posted on 2009-05-05
5
Medium Priority
?
775 Views
Last Modified: 2012-05-06
I am trying to copy one file to another, replacing a particular bit of text.  The original text has blank lines and lines beginning with semicolons, all of which I want to keep.  How can I accomplish this?  This code is what I have so far, but it ignores blank lines.

Thanks!!
for /F "eol= tokens=1-2 delims=" %%x in (orig.ini) do (
  for /F "eol= tokens=1-6 delims=" %%I in ("%%x") do (
    IF %%I==TEXT1 (
      echo TEXT1=NEWVALUE>>"new.ini"
    ) ELSE (
      echo %%x>>"new.ini"
    )
  )
)

Open in new window

0
Comment
Question by:brs5tettba
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 21

Accepted Solution

by:
AmazingTech earned 750 total points
ID: 24311823
I think this should work for you
for /F "tokens=1,* delims=]" %%x in ('type orig.ini ^| find /v /n ""') do (
    IF %%y==TEXT1 (
        echo TEXT1=NEWVALUE>>"new.ini"
    ) ELSE (
        if "%%y" == "" (
            echo.>>"new.ini"
        ) ELSE (
            echo %%y>>"new.ini"
        )
    )
)

Open in new window

0
 

Author Comment

by:brs5tettba
ID: 24318196
I used your suggestion but put the blank case first (if "%%y" == ""), and that fixed it for me.  Creative answer, thank you!

for /F "tokens=1,* delims=]" %%x in ('type orig.ini ^| find /v /n ""') do (
    if "%%y" == "" (
        echo.>>"new.ini"
    ) ELSE (
        IF %%y==TEXT1 (
            echo TEXT1=NEWVALUE>>"new.ini"
        ) ELSE (
            echo %%y>>"new.ini"
        )
    )
)

Open in new window

0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24319202
OK. Great.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24319229
Another trick I picked up here is to create the new.ini file like this.

I think this should work with your script.

This way you don't need to delete the previous new.ini if one already exist.
ECHO OFF
(for /F "tokens=1,* delims=]" %%x in ('type orig.ini ^| find /v /n ""') do (
    if "%%y" == "" (
        echo.
    ) ELSE (
        IF %%y==TEXT1 (
            echo TEXT1=NEWVALUE
        ) ELSE (
            echo %%y
        )
    )
))>"new.ini"

Open in new window

0
 
LVL 16

Expert Comment

by:t0t0
ID: 24623177
AmazingTech

EXCELLENT !!!!!

I have been pulling my hair out for the past ..... hours trying to find a solution to this very problem.

You are the KING!!
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

609 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