Creating a series of text files

frozey

New member
Joined
Jun 27, 2011
Messages
2
Programming Experience
Beginner
I am trying to write a program that writes information from texts boxes to a text file on the users hard drive. I would like to create a new text file if one already exists, for example if text1.text exists, it creates a new text file called text2.text, then if text2.text exists it creates text3.text and so on. Here is what I have so far:

Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim FILE_NAME As String = "C:\Users\Sean\Desktop\Test.txt"
        Dim objWriter As New System.IO.StreamWriter(FILE_NAME, append:=True)
        objWriter.Close()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FILE_NAME As String = "C:\Users\Sean\Desktop\Test.txt"

        If System.IO.File.Exists(FILE_NAME) = True Then
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, append:=True)
            objWriter.Write(TextBox1.Text)
            objWriter.Close()
            MsgBox("Text written to file")
        Else
            MsgBox("File Does Not Exist")
        End If
    End Sub


End Class


I've tried fooling around with if statements to create a new file but it doesn't seem to be working.

Additionally, How can I write to a new line in a text file. For example there are 4 text boxes and each string from each text box gets its own line.

Thank you very much for all your time and help!
 
Something like this:
Dim fileNumber As Integer = 1

Do While IO.File.Exists(String.Format("C:\SomeFolder\Text{0}.txt", fileNumber))
    fileNumber += 1
Loop

Dim filePath As String = String.Format("C:\SomeFolder\Text{0}.txt", fileNumber)
 
Back
Top