Solved

Scan and replace character string in file

Posted on 2014-12-14
5
163 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 45

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 35

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 54

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

752 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