Results 1 to 3 of 3

Thread: Read / Write To Text File

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Member caddog71's Avatar
    Join Date
    Paramount, California
    Login to Give a bone

    Cool Read / Write To Text File

    I am trying to read a ".txt" file and append to the end of the file to create an on going list. I have found some examples from previous issues on this forum but I am have trouble with my output in the text file not exiting the loop. I am guessing that the problem is with the "Do Until sdr.AtEndOfStream" line. Can someone explain what I am doing wrong, and where could I reference something to fix the problem. Thank you in advance.

    Public Sub WriteFile(strMapNumber, str200Map)
    Dim FileName As String
    FileName = "C:\Temp\Duplicate_OH.txt"
    If Dir(FileName) <> "" Then
        Const ForReading = 1, ForWriting = 2, ForAppending = 8
        Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
        Dim fso As Object
        Dim fdw As Object, sdw As Object
        Dim fdr As Object, sdr As Object
        Dim strText As String
         'create file sysytem object
        Set fso = CreateObject("Scripting.FileSystemObject")
         'get file to read and open text stream
        Set fdr = fso.GetFile(FileName)
        Set sdr = fdr.OpenAsTextStream(ForReading, TristateUseDefault)
         'create new file to write to
        fso.CreateTextFile (FileName)
        Set fdw = fso.GetFile(FileName)
        Set sdw = fdw.OpenAsTextStream(ForAppending, TristateUseDefault)
        'iterate through the end of first file
        Do Until sdr.AtEndOfStream
            'read string from the first file
            strText = sdr.ReadLine & vbNewLine  '<--added carriage return to jump on the next line
            'write to the second one
            sdw.write strText & "100 Scale: " & strMapNumber & vbTab & "200 Scale: " & str200Map & vbNewLine
        'close both files and clean up
        Set sdr = Nothing
        Set sdw = Nothing
        Set fdr = Nothing
        Set fdw = Nothing
        Set fso = Nothing
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set fdw = fso.CreateTextFile("C:\Temp\Duplicate_OH.txt", True)
        fdw.writeline ("This Is A Test1.")
    End If
    End Sub
    Last edited by Ed Jobe; 2008-05-23 at 02:06 PM. Reason: Added Code tags.

Similar Threads

  1. Create a list of drawings and write them to a text file
    By aaronic_abacus in forum AutoLISP
    Replies: 19
    Last Post: 2014-11-26, 07:36 AM
  2. 2013: Placing user read/write permissions on central file??
    By graphite in forum Revit - Worksharing/Worksets/Revit Server
    Replies: 11
    Last Post: 2013-11-18, 11:53 PM
  3. Read File from text file to restore settings
    By dilek in forum AutoCAD Customization
    Replies: 1
    Last Post: 2013-04-22, 12:47 PM
  4. Write usernames to text file
    By mikelf in forum AutoLISP
    Replies: 8
    Last Post: 2009-01-31, 09:33 PM
  5. Replies: 10
    Last Post: 2007-10-09, 01:11 PM

Posting Permissions

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