alterning the textfile

Results 1 to 2 of 2

Thread: alterning the textfile

  1. #1
    Join Date
    Dec 1969

    Default alterning the textfile

    I need to change the text in the text file like<BR><BR>Set MyFile =MyFileObject.OpentextFile (C: extFile.txt,8)<BR>while not MyFile.AtEndofStream <BR>MyFile.Readline<BR>if MyFile.Readline = somevalue<BR><BR>...if the MyFile.Readline gives me somevalue, i want to change the value in the textFile.<BR><BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: alterning the textfile

    I don&#039t know if this is the best way to do it, but...<BR><BR>If what you want to do is simply replace one fixed string with another, then you should consider using a regular expression (see: for an overview of regular expressions in VBScript). The method you could use then would look something like:<BR><BR>&#039 First create the regular expression<BR>Dim objRegExp<BR>Set objRegExp = New RegExp <BR> <BR>objRegExp.IgnoreCase = True<BR>objRegExp.Pattern = myPattern &#039The pattern you want to replace<BR> <BR>strReplace = myNewString &#039What you want to replace it with<BR><BR>&#039 Then open your file for reading...<BR>Set MyFile =MyFileObject.OpenTextFile (C: extFile.txt,1)<BR>&#039 Read throught the file<BR>Do while not MyFile.AtEndofStream <BR> myText = MyFile.Readline<BR> myText = objRegExp.Replace(myText, strReplace) &#039Do the replaceif the regexp matches<BR> strStore = strStore & myText & vbCrLf &#039 Store the results<BR>Loop<BR><BR>&#039Close your file and clean up<BR>MyFile.Close<BR>Set objRegExp = Nothing<BR><BR>&#039Open your file for writing and write out strStore to the file<BR>Set MyFile =MyFileObject.OpenTextFile (C: extFile.txt,2)<BR>MyFile.Write strStore<BR>MyFile.Close &#039Close MyFile <BR> <BR>If you don&#039t want to use a simple fixed search and replace method as described above, then I guess that you will have to have an intermediate step when you prompt the user for input from a form or something.<BR><BR>As far as I can tell, you have to open the file for reading, read the data from it, close and then reopen the file to write out the corrected version. I will, however, be happily corrected.<BR><BR>Hope that this helps.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts