Solved

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

Posted on 2009-05-05
5
725 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
  • 3
5 Comments
 
LVL 21

Accepted Solution

by:
AmazingTech earned 250 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

911 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

23 Experts available now in Live!

Get 1:1 Help Now