Solved

Scan and replace character string in file

Posted on 2014-12-14
5
157 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
5 Comments
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 167 total points
Comment Utility
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
Comment Utility
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 51

Accepted Solution

by:
Bill Prew earned 167 total points
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 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

9 Experts available now in Live!

Get 1:1 Help Now