How to clean all the txt in a txt file and then put a paragraph in it

Hi, Experts,

Is there any way to clean all the txt in the txt file firts and then put a paragraph in it.

Thanks,

Turbot
turbot_yuAsked:
Who is Participating?
 
Ryan ChongConnect With a Mentor Commented:
You can directly overwrite that file, like this:

Private Sub WriteFileText(ByVal FileName As String, ByVal Source As String, Optional noCrlf As Boolean = False, Optional Append As Boolean = False)
    On Error GoTo EHandler
    Dim Handle As Integer
    Handle = FreeFile
    If Append Then
        Open FileName For Append As #Handle
            If noCrlf Then
                Print #Handle, Source;
            Else
                Print #Handle, Source
            End If
            On Error Resume Next
        Close #Handle
    Else
        Open FileName For Output As #Handle
            If noCrlf Then
                Print #Handle, Source;
            Else
                Print #Handle, Source
            End If
            On Error Resume Next
        Close #Handle
    End If
    Exit Sub
EHandler:
    'ShowErrMsg
    On Error Resume Next
    Close #Handle
End Sub

then try like:

myContent = Text1.Text
WriteFileText "C:\test.txt", myContent , True

get the idea?
0
 
MarioLeoCommented:
What I always do is simply close the file, kill it and then create another file with the new content : 3 lines of code, hassle -free

Regards,

Mario
0
 
Shiju SasidharanConnect With a Mentor Assoc Project ManagerCommented:
hi
or u can use FileSystemObjects
its easy
'Add reference
'Project Menu -> References -> Microsoft Scripting Runtime
'============================================

Private Sub Command1_Click()
    'Usage
        Call OverWriteFile("C:\MyFile.txt", "This is new data")
End Sub
Private Sub OverWriteFile(ByVal FileName As String, ByVal DataToBeWritten As String)
On Error GoTo Last
    Dim objFso As New FileSystemObject
    Dim objTextStream As TextStream
       
    Set objTextStream = objFso.OpenTextFile(FileName, ForWriting, True)
    objTextStream.Write DataToBeWritten
    objTextStream.Close
    Set objFso = Nothing
    MsgBox "File Updated", vbInformation
Last:
    If Err.Number <> 0 Then
        MsgBox Err.Description
        Err.Clear
    End If
End Sub
'====================================

;-)
Shiju
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ShawnCorbettCommented:
Instead of opening the file in append mode just open it in output mode and it will overwrite whatever is there.

open "Filename.txt" for output as #1
write #1, "paragraph"
close #1
0
 
nffvrxqgrcfqvvcCommented:
Its been more than 21 days since a comment has been made. I am leaving this for cleanup.
Recommend: Split: ryancys , shijusn
0
 
nffvrxqgrcfqvvcCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I will leave the following recommendation for this question in the Cleanup topic area:

[Split: ryancys and shijusn]

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

egl1044
EE Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.