Results 1 to 3 of 3

Thread: Read / Write To Text File

  1. #1
    Member caddog71's Avatar
    Join Date
    2007-02
    Location
    Paramount, California
    Posts
    35
    Login to Give a bone
    0

    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.

    Code:
    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
            
        Loop
          
        'close both files and clean up
        sdr.Close
        sdw.Close
        Set sdr = Nothing
        Set sdw = Nothing
        Set fdr = Nothing
        Set fdw = Nothing
        Set fso = Nothing
            
    Else
    
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set fdw = fso.CreateTextFile("C:\Temp\Duplicate_OH.txt", True)
        
        fdw.writeline ("This Is A Test1.")
        fdw.Close
    
    End If
    
    End Sub
    Last edited by Ed Jobe; 2008-05-23 at 02:06 PM. Reason: Added Code tags.

  2. #2
    I could stop if I wanted to
    Join Date
    2002-02
    Location
    Kansas
    Posts
    487
    Login to Give a bone
    0

    Default Re: Read / Write To Text File

    check in vba help for Open statement
    1. you can set the open mode to append and the access to write.
    2. then out put the data you need to append using print. #
    3. and close the file
    Note:
    update to 2011
    I am using ADT 2007 08/06/2008

    PS. If you use any Code I post or Down Load Attachment I would like to know if it was helpful. So please post reply to this thread, on how you use the code or down load. Thank You

  3. #3
    AUGI Addict MikeJarosz's Avatar
    Join Date
    2015-10
    Location
    New York NY
    Posts
    1,497
    Login to Give a bone
    0

    Default Re: Read / Write To Text File

    File scripting is powerful but, as you have found out, complicated. Open for input/output is much simpler.

    Here's an example of opening text files for input and output. The input file is a list of drawings to process. The program reads the filenames from this file, one at a time. Then it loads the file, prints the filename and the layout count to the output file then cycles through and prints the name of each layout. It then closes the drawing file. Next, it loops back to process the next file on the list, until it reaches the end of the input file. After all drawings are processed it closes the two text files and exits.

    Substitute your own network drives for mine, or you could use c:\.




    Code:
     
    Sub LayoutLoop()
    Dim FileName As String
    Dim F As Integer
    Dim G As Integer
    Dim Layouts As AcadLayouts, Layout As AcadLayout
    F = FreeFile()
    Open "t:\temp\filelist.txt" For Input As #F
    G = FreeFile()
    Open "t:\temp\LayoutReport.txt" For Output As #G
    Do While Not EOF(F)
        Line Input #F, FileName
        ThisDrawing.Application.Documents.Open FileName, ReadOnly
     
        Set Layouts = ThisDrawing.Layouts
        Print #G, FileName & " -- Layouts:" & Layouts.Count
     
        For Each Layout In Layouts
            Print #G, Tab; Layout.Name
        Next Layout
        ThisDrawing.Close , savechanges = False
    Loop
    Close #F
    Close #G
    Debug.Print "DONE"
    End Sub
    "everything you know is wrong...." -FST

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
  •