Solved

Scan and replace character string in file

Posted on 2014-12-14
5
160 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
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 53

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This is about my first experience with programming Arduino.
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…

825 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