Write to Notepad keeps growing every time the rule is run

Write to Notepad keeps growing every time the rule is run

RoyWickrama_RWEI
Advisor Advisor
780 Views
7 Replies
Message 1 of 8

Write to Notepad keeps growing every time the rule is run

RoyWickrama_RWEI
Advisor
Advisor

As a part of my rule, I need to write data into a text file. I noticed that, it keeps growing instead of creating a new text file.

Could someone help, please!

Thanks.

Public Class forBomStructure
	Shared oTextSave As String = "C:\Users\Public\Documents\iLogicBuffer.txt"
			
	Sub Main()
'	create_txt(oWritex)
		Dim oDoc As Document = ThisApplication.ActiveDocument
		Dim oModelDoc As Document
		Dim oStr As String = ""
		Dim iL_Name as String = "0023A Full File Names of Occurrences"
			oWrite_Sub(oWrite, "FILE REFERENCE TREE RAN FROM: " & oDoc.FullFileName)			
			oWrite_Sub(oWrite, "RULE: " & iL_Name)	
			oWrite_Sub(oWrite, "-----------------------------")
'			Process.Start ("Notepad.exe", oTextSave)
'			Exit Sub
		
			For Each oModelDoc In oDoc.AllReferencedDocuments
''				RecurReferencing(oModelDoc, oStr, 0)
'				MessageBox.Show("oModelDoc.Name: " & oModelDoc.FullFileName, "Title")
'				oFFN_Document = oModelDoc.FullFileName
'				oWrite_Sub(oWrite, oFFN_Document)
			Next
			
		Process.Start ("Notepad.exe", oTextSave)
	
	End Sub
	
	Sub oWrite_Sub(oWrite As System.IO.StreamWriter, oStr_to_Write As String)

		oWrite = System.IO.File.AppendText(oTextSave)
		oWrite.WriteLine(oStr_to_Write)	
		oWrite.Flush()
		oWrite.Close

	If My_Expression Then
		'---------------
		End If
	End Sub
		
		
End Class
0 Likes
Accepted solutions (1)
781 Views
7 Replies
Replies (7)
Message 2 of 8

GeorgK
Advisor
Advisor
Accepted solution

If you need everytime a new file than you can delete the old one before writing to it:

 

Dim FileDelete As String

FileDelete = "C:\testDelete.txt"

 If System.IO.File.Exists( FileDelete ) = True Then
   System.IO.File.Delete( FileDelete )
   MsgBox("File Deleted")
End If
Message 3 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support

@RoyWickrama_RWEI ,

 

@GeorgK  is right. If text file is already exists, delete before creating this line (create_txt(oWritex)).

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 4 of 8

RoyWickrama_RWEI
Advisor
Advisor

Great help. It works.

Thanks. I added the following in my code:

 

	Sub oDelete_ex_Notepad(oTextSave As String)   'To delete oTextSave if existing.
		Dim FileDelete As String
		FileDelete = "C:\testDelete.txt"
	 	If System.IO.File.Exists(oTextSave) = True Then
	   	System.IO.File.Delete(oTextSave)
	   	MsgBox("File Deleted")
		End If
	End Sub
0 Likes
Message 5 of 8

forbillian
Advocate
Advocate

Hi Chandra,

 

I am trying to create a text file which gets all part names in an assembly written, however I am struggling to

work out how to combine both write & read of my text file lines and only add the next part name if it doesn't already exist in previous lines.

 

Here is a basic example where I can check if the name exists - but I am unsure how to then activate the append function with it (you'll see it commented out below) so I can then add the name if its unique:

 

oFile = "C:\Temp\Names.txt"
oRead = System.IO.File.OpenText(oFile)
Dim sLine As String = ""

Do
	sLine2 = ThisDoc.FileName(False)
    sLine = oRead.ReadLine()
    If Not sLine Is Nothing Then
       If sLine <> sLine2 Then
		   
'		   oAppend = IO.File.AppendText("C:\TEMP2\Test_File.txt")
'		   oAppend.WriteLine(ThisDoc.FileName(False))
'           oAppend.Flush()
'		   oAppend.Close()

		   MsgBox(sLine2)
	   End If
    End If
Loop Until sLine Is Nothing
oRead.Close()
ThisDoc.Launch(oFile)

 Any help would be appreciated. Thanks @chandra.shekar.g 

0 Likes
Message 6 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support

@forbillian,

 

Try below iLogic code to write part name if does not exists.

Dim name As String = ThisDoc.FileName(False)
Dim Path As String = "C:\temp\Testing.txt"
Dim file As System.IO.StreamReader = New System.IO.StreamReader(path)
Dim found As Boolean = False 

For Each Line As String In System.IO.File.ReadAllLines(path) 
    If Line.Contains(name) = True Then
		found = True 
        Exit For
    End If
Next
file.Close()

If found = False Then
	oAppend = System.IO.File.AppendText(path)
	oAppend.WriteLine(ThisDoc.FileName(False))
	oAppend.Flush()
	oAppend.Close()
End If

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 7 of 8

forbillian
Advocate
Advocate

Thankyou so much @chandra.shekar.g

 

That has saved me so much time.  I was originally sending each line to an excel file but

it is so much faster sending it to a txt file.  I was just stuck on that duplicate name issue.

 

Much appreciated!

 

 

0 Likes
Message 8 of 8

_dscholtes_
Advocate
Advocate

@RoyWickrama_RWEI (@GeorgK ) Instead of

System.IO.File.AppendText(oTextSave)

 

try

System.IO.File.CreateText(oTextSave)

 

 

According to this microsoft API article, :

Creates or opens a file for writing UTF-8 encoded text. If the file already exists, its contents are overwritten.

 

 

 

0 Likes