Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Scan and replace character string in file

Posted on 2014-12-14
5
Medium Priority
?
168 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 668 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 664 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 57

Accepted Solution

by:
Bill Prew earned 668 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Progress
Starting up a Project

688 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