Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2003
    Location
    New York
    Posts
    160

    Unanswered: FSO - Appending Replacements

    i am trying to read in a text file and replace certain words and write tit to a new text file. The problem im getting is how to append all the replacements. I tried readall and readline. I have to replace around 10 words. This is what i go so far

    mf1 mf2 mf3 are all dim as string
    Dim fso, txtOrg, txtNew
    Dim oTmp As String, iTmp As String
    Dim oStream, iStream
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set txtOrg = fso.GetFile("X:\programming\genericER.txt")
    Set txtNew = fso.GetFile("X:\programming\test.txt")
    Set iStream = txtOrg.OpenAsTextStream(1)
    Set oStream = txtNew.OpenAsTextStream(8)
    Do While Not iStream.AtEndOfStream
    iTmp = iStream.readall ' readall or readline ?
    mf1 = Replace(iTmp, "[MAGAZINENAME]", UCase(txtMagName.Text), , 1)
    mf2 = Replace(iTmp, "[MAGAZINETYPE]", txtMagType.Text), , 1)
    oStream.write mf1 ' write or writeline ?
    Loop

    i am stuck i tired a few different variants of this.

    thnx
    Beyond Limitation

  2. #2
    Join Date
    Mar 2003
    Location
    Atlanta, GA
    Posts
    191

    Re: FSO - Appending Replacements

    Originally posted by vextout
    i am trying to read in a text file and replace certain words and write tit to a new text file. The problem im getting is how to append all the replacements. I tried readall and readline. I have to replace around 10 words. This is what i go so far

    mf1 mf2 mf3 are all dim as string
    Dim fso, txtOrg, txtNew
    Dim oTmp As String, iTmp As String
    Dim oStream, iStream
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set txtOrg = fso.GetFile("X:\programming\genericER.txt")
    Set txtNew = fso.GetFile("X:\programming\test.txt")
    Set iStream = txtOrg.OpenAsTextStream(1)
    Set oStream = txtNew.OpenAsTextStream(8)
    Do While Not iStream.AtEndOfStream
    iTmp = iStream.readall ' readall or readline ?
    mf1 = Replace(iTmp, "[MAGAZINENAME]", UCase(txtMagName.Text), , 1)
    mf2 = Replace(iTmp, "[MAGAZINETYPE]", txtMagType.Text), , 1)
    oStream.write mf1 ' write or writeline ?
    Loop

    i am stuck i tired a few different variants of this.

    thnx
    I think I understand your need... I would do it this way...

    Dim fso, f1, ts, s
    Const ForReading = 1
    Const ForWriting = 2
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.OpenTextFile("c:\test.txt", ForReading)
    s = ""
    Do While Not ts.AtEndOfStream
    s = s & ts.ReadLine
    Loop
    ts.Close
    Set ts = Nothing
    Set ts = fso.OpenTextFile("c:\test.txt", ForWriting)
    s = Replace(s, "[MAGAZINENAME]", UCase(txtMagName.Text))
    s = Replace(s, "[MAGAZINETYPE]", UCase(txtMagType.Text))
    ts.Write s
    ts.Close
    Set ts = Nothing

    I wrote this up from my head. I think it will work... you may have to add a parameter in the fso statement depending on the type of file format you're working with. Hope this helps...

    Tim
    Tim

  3. #3
    Join Date
    Jan 2003
    Location
    New York
    Posts
    160
    i am sorry i forgot to reply to this thread. i got ti wokring middle of last week.

    i jsut did it like this.

    Do While Not iStream.AtEndOfStream
    mf = ""
    iTmp = iStream.readline
    If InStr(iTmp, "[MAGAZINENAME]") And mf = "" Then
    mf = Replace(iTmp, "[MAGAZINENAME]", txtMagName.Text, , 1)
    ElseIf InStr(iTmp, "[MAGAZINENAME]") Then
    mf = Replace(mf, "[MAGAZINENAME]", txtMagName.Text, , 1)
    End If
    If InStr(iTmp, "[MAGAZINETYPE]") And mf = "" Then
    mf = Replace(iTmp, "[MAGAZINETYPE]", txtMagType.Text, , 1)
    ElseIf InStr(iTmp, "[MAGAZINETYPE]") Then
    mf = Replace(mf, "[MAGAZINETYPE]", txtMagType.Text, , 1)
    End If

    and so on for another 10 if statments - it work


    thx for reply though
    sorry again
    Beyond Limitation

Posting Permissions

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