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
Re: Read / Write To Text File
check in vba help for Open statement
- you can set the open mode to append and the access to write.
- then out put the data you need to append using print. #
- and close the file
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