Solved

Scan and replace character string in file

Posted on 2014-12-14
5
164 Views
Last Modified: 2015-01-03
I need to scan this file for a string and also change certain strings.  I believe I need to use the opentextfile method.  I have been unable to determine how.  If possible, could someone point me in the correct direction?   Below is as close as I have gotten to code that will work.  I am open to any suggestions or better methods.  Thanks again.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, MyFile, FileName, TextLine

Set fso = CreateObject("Scripting.FileSystemObject")

' Open the file for output.
FileName = "c:\scripts\test.cfg"



' Open the file for input.
Set MyFile = fso.OpenTextFile(FileName, ForReading)

' Read from the file and display the results.
Do While MyFile.AtEndOfStream <> True
    TextLine = MyFile.ReadLine
    msgbox TextLine & "<br />"
Loop
MyFile.Close

Open in new window

IDAPI32.txt
0
Comment
Question by:hardrock95
[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
5 Comments
 
LVL 46

Assisted Solution

by:aikimark
aikimark earned 167 total points
ID: 40499729
What program or code created this file and what program or code will use the file after you have changed it?  It is in a binary format, so this could be tricky.

I'm pretty sure, I will be recommending you invoke the ReadAll method from the MyFile textstream object.  Trying to read your binary file with readline isn't going to work.

You will need to close the input file and reopen it for output and write the changed contents.
0
 
LVL 36

Assisted Solution

by:Kimputer
Kimputer earned 166 total points
ID: 40500023
You're already on the right track (but as above Expert mentioned, if text file).
I never change the text file directly either.
I'd do the readline (original text), writeline (with temp file, with string find/replace code) method.
When found, have a flag set.
If not found, do nothing (delete temp file), if found, close both files, delete original, move temp file to original.
0
 
LVL 56

Accepted Solution

by:
Bill Prew earned 167 total points
ID: 40500224
Take a look at this solution I posted in a different question, it should give you some ideas.  Let me know if you have questions.

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28161581.html#a39260393

~bp
0
 

Author Closing Comment

by:hardrock95
ID: 40529612
I apologize for the delay in responding, thanks to everyone.  I am not sure how you guys are rewarded, but it is not enough I am sure.  Thanks again for everything you experts do.
Troy
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

636 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