12 Replies - 4995 Views - Last Post: 03 February 2013 - 01:10 PM Rate Topic: -----

#1 kcorj2244  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-January 13

Create new line when writing to a txt file

Posted 02 February 2013 - 06:02 PM

How would I add a new line to the textfile, instead of erasing everything I currently have


here is my current code

        Dim inputData As String


        inputData = InputBox("Enter what to save")

        Dim FILE_NAME As String = "C:\Users\Ben\Documents\Vb.netshiz\learninggoal.txt"

        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objWriter As New System.IO.StreamWriter(FILE_NAME)

            objWriter.WriteLine(vbNewLine & inputData)
            objWriter.Close()
            MsgBox("Text written to file")

        Else

            MsgBox("File Does Not Exist, being created now")
            Dim writeFile As IO.StreamWriter
            writeFile = IO.File.CreateText(FILE_NAME) 'Creates a new file
            writeFile.WriteLine(inputData)
            writeFile.Close()




        End If







Feel free to fix up the code also if you want, because I know it's messy :P

Is This A Good Question/Topic? 0
  • +

Replies To: Create new line when writing to a txt file

#2 kcorj2244  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-January 13

Re: Create new line when writing to a txt file

Posted 02 February 2013 - 06:21 PM

Ok, so I got it working

        Dim inputData As String


        inputData = InputBox("Enter what to save")

        Dim FILE_NAME As String = "C:\Users\Ben\Documents\Vb.netshiz\learninggoal.txt"



        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, IO.FileMode.Append)

            objWriter.WriteLine(inputData)
            objWriter.Close()
            MsgBox("Text written to file")

        Else

            MsgBox("File Does Not Exist, being created now")
            Dim writeFile As IO.StreamWriter
            writeFile = IO.File.CreateText(FILE_NAME) 'Creates a new file
            writeFile.WriteLine(inputData)
            writeFile.Close()




        End If



I have another question now. How do I let the user specify a custom file name, but have it stay in the same directory?
Was This Post Helpful? 0
  • +
  • -

#3 LilGhost  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

Re: Create new line when writing to a txt file

Posted 02 February 2013 - 07:34 PM

when you go to write to the document say:

FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.text.replace("\", "").replace(".txt", "") & ".txt"



and then continue with your saving stuff. Or, if you'd like to get the filename with an input box say something like:
Dim saveSpot as string = inputbox("What should the file name be?")
FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & saveSpot.replace("\", "").replace(".txt", "") & ".txt"


Was This Post Helpful? 0
  • +
  • -

#4 kcorj2244  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-January 13

Re: Create new line when writing to a txt file

Posted 03 February 2013 - 10:45 AM

View PostLilGhost, on 02 February 2013 - 07:34 PM, said:

when you go to write to the document say:

FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.text.replace("\", "").replace(".txt", "") & ".txt"



and then continue with your saving stuff. Or, if you'd like to get the filename with an input box say something like:
Dim saveSpot as string = inputbox("What should the file name be?")
FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & saveSpot.replace("\", "").replace(".txt", "") & ".txt"



Ok, I have another question to add.

Here is my current code

        Dim inputData As String


        inputData = InputBox("Enter what to save")

        Dim FILE_NAME As String = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"



        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, IO.FileMode.Append)

            objWriter.WriteLine(inputData)
            objWriter.Close()
            MsgBox("Text written to file")



        Else

            MsgBox("File Does Not Exist, being created now")

            FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"

            Dim writeFile As IO.StreamWriter
            writeFile = IO.File.CreateText(FILE_NAME) 'Creates a new file
            writeFile.WriteLine(inputData)
            writeFile.Close()



        End If



So right now, the file that it saves to has to be specified in the textbox. For example, if you would like to create a new file you would put w/e you wanted in the text box, but if you wanted to save to an existing file, you would have to specify the existing file name minus the extension. So, my question is how do I create a default file name, that it will create/write to, if the text box is empty? What is the code to see if the textbox is empty?

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 kcorj2244  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-January 13

Re: Create new line when writing to a txt file

Posted 03 February 2013 - 10:54 AM

View Postkcorj2244, on 03 February 2013 - 10:45 AM, said:

View PostLilGhost, on 02 February 2013 - 07:34 PM, said:

when you go to write to the document say:

FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.text.replace("\", "").replace(".txt", "") & ".txt"



and then continue with your saving stuff. Or, if you'd like to get the filename with an input box say something like:
Dim saveSpot as string = inputbox("What should the file name be?")
FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & saveSpot.replace("\", "").replace(".txt", "") & ".txt"



Ok, I have another question to add.

Here is my current code

        Dim inputData As String


        inputData = InputBox("Enter what to save")

        Dim FILE_NAME As String = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"



        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, IO.FileMode.Append)

            objWriter.WriteLine(inputData)
            objWriter.Close()
            MsgBox("Text written to file")



        Else

            MsgBox("File Does Not Exist, being created now")

            FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"

            Dim writeFile As IO.StreamWriter
            writeFile = IO.File.CreateText(FILE_NAME) 'Creates a new file
            writeFile.WriteLine(inputData)
            writeFile.Close()



        End If



So right now, the file that it saves to has to be specified in the textbox. For example, if you would like to create a new file you would put w/e you wanted in the text box, but if you wanted to save to an existing file, you would have to specify the existing file name minus the extension. So, my question is how do I create a default file name, that it will create/write to, if the text box is empty? What is the code to see if the textbox is empty?

Thanks


Ok, I just got everything working... Now I have a new request, not entirely related to this.

I want it so the user can specify what the default file name is, and then have that change this part of my code:

If TextBox1.Text = String.Empty Then
            FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\learninggoal.txt"
        End If



to w/e they want. For example, if they click specify default (i will make a button that says that)

they enter in the default file name that they want to be default, and then have it change the code to this

If TextBox1.Text = String.Empty Then
            FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\WHAT_THE_USER_SPECIFYS_AS_THE_DEFAULT.txt"
        End If



and then save the program code so I can just exit out of visual studio.
Was This Post Helpful? 0
  • +
  • -

#6 LilGhost  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

Re: Create new line when writing to a txt file

Posted 03 February 2013 - 10:55 AM

View Postkcorj2244, on 03 February 2013 - 10:45 AM, said:

View PostLilGhost, on 02 February 2013 - 07:34 PM, said:

when you go to write to the document say:

FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.text.replace("\", "").replace(".txt", "") & ".txt"



and then continue with your saving stuff. Or, if you'd like to get the filename with an input box say something like:
Dim saveSpot as string = inputbox("What should the file name be?")
FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & saveSpot.replace("\", "").replace(".txt", "") & ".txt"



Ok, I have another question to add.

Here is my current code

        Dim inputData As String


        inputData = InputBox("Enter what to save")

        Dim FILE_NAME As String = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"



        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, IO.FileMode.Append)

            objWriter.WriteLine(inputData)
            objWriter.Close()
            MsgBox("Text written to file")



        Else

            MsgBox("File Does Not Exist, being created now")

            FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"

            Dim writeFile As IO.StreamWriter
            writeFile = IO.File.CreateText(FILE_NAME) 'Creates a new file
            writeFile.WriteLine(inputData)
            writeFile.Close()



        End If



So right now, the file that it saves to has to be specified in the textbox. For example, if you would like to create a new file you would put w/e you wanted in the text box, but if you wanted to save to an existing file, you would have to specify the existing file name minus the extension. So, my question is how do I create a default file name, that it will create/write to, if the text box is empty? What is the code to see if the textbox is empty?

Thanks


Dim inputData As String
        inputData = InputBox("Enter what to save")

If Textbox1.text <> "" Then

        Dim FILE_NAME As String = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"

Else
Dim FILE_NAME As String = "C:\Users\Ben\Documents\Vb.netshiz\DefaultSaveSpot.txt"
End If

        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, IO.FileMode.Append)

            objWriter.WriteLine(inputData)
            objWriter.Close()
            MsgBox("Text written to file")



        Else

            MsgBox("File Does Not Exist, being created now")

            FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"

            Dim writeFile As IO.StreamWriter
            writeFile = IO.File.CreateText(FILE_NAME) 'Creates a new file
            writeFile.WriteLine(inputData)
            writeFile.Close()



        End If



or you can simply move around that if statement so it gives them an error message and doesn't execute any code (besides the error message) when the textbox1.text element = "" (empty string).
Was This Post Helpful? 0
  • +
  • -

#7 kcorj2244  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-January 13

Re: Create new line when writing to a txt file

Posted 03 February 2013 - 11:06 AM

View PostLilGhost, on 03 February 2013 - 10:55 AM, said:

View Postkcorj2244, on 03 February 2013 - 10:45 AM, said:

View PostLilGhost, on 02 February 2013 - 07:34 PM, said:

when you go to write to the document say:

FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.text.replace("\", "").replace(".txt", "") & ".txt"



and then continue with your saving stuff. Or, if you'd like to get the filename with an input box say something like:
Dim saveSpot as string = inputbox("What should the file name be?")
FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & saveSpot.replace("\", "").replace(".txt", "") & ".txt"



Ok, I have another question to add.

Here is my current code

        Dim inputData As String


        inputData = InputBox("Enter what to save")

        Dim FILE_NAME As String = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"



        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, IO.FileMode.Append)

            objWriter.WriteLine(inputData)
            objWriter.Close()
            MsgBox("Text written to file")



        Else

            MsgBox("File Does Not Exist, being created now")

            FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"

            Dim writeFile As IO.StreamWriter
            writeFile = IO.File.CreateText(FILE_NAME) 'Creates a new file
            writeFile.WriteLine(inputData)
            writeFile.Close()



        End If



So right now, the file that it saves to has to be specified in the textbox. For example, if you would like to create a new file you would put w/e you wanted in the text box, but if you wanted to save to an existing file, you would have to specify the existing file name minus the extension. So, my question is how do I create a default file name, that it will create/write to, if the text box is empty? What is the code to see if the textbox is empty?

Thanks


Dim inputData As String
        inputData = InputBox("Enter what to save")

If Textbox1.text <> "" Then

        Dim FILE_NAME As String = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"

Else
Dim FILE_NAME As String = "C:\Users\Ben\Documents\Vb.netshiz\DefaultSaveSpot.txt"
End If

        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, IO.FileMode.Append)

            objWriter.WriteLine(inputData)
            objWriter.Close()
            MsgBox("Text written to file")



        Else

            MsgBox("File Does Not Exist, being created now")

            FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"

            Dim writeFile As IO.StreamWriter
            writeFile = IO.File.CreateText(FILE_NAME) 'Creates a new file
            writeFile.WriteLine(inputData)
            writeFile.Close()



        End If



or you can simply move around that if statement so it gives them an error message and doesn't execute any code (besides the error message) when the textbox1.text element = "" (empty string).



So this is my code right now, it's basically finished

        Dim inputData As String


        inputData = InputBox("Enter what to save")

        Dim FILE_NAME As String = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"

        If TextBox1.Text = String.Empty Then
            FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\learninggoal.txt"
        End If

        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, IO.FileMode.Append)

            objWriter.WriteLine(inputData)
            objWriter.Close()
            MsgBox("Text written to file")



        Else

            Dim writeFile As IO.StreamWriter

            If TextBox1.Text = String.Empty Then
                FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\learninggoal.txt"

                writeFile = IO.File.CreateText(FILE_NAME) 'Creates a new file
                writeFile.WriteLine(inputData)
                writeFile.Close()

            End If

            MsgBox("File Does Not Exist, being created now")

            FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\" & TextBox1.Text.Replace("\", "").Replace(".txt", "") & ".txt"


            writeFile = IO.File.CreateText(FILE_NAME) 'Creates a new file
            writeFile.WriteLine(inputData)
            writeFile.Close()



        End If
[code]

The only thing I need is that if the textbox is empty as seen here, I want it to stop all the code after that. What do I use? I tried end but that closes my program. How do I stop the code from being executed after that if statement, if that if statement is infact used (ie: the textbox is empty)

[code]
If TextBox1.Text = String.Empty Then
                FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\learninggoal.txt"

                writeFile = IO.File.CreateText(FILE_NAME) 'Creates a new file
                writeFile.WriteLine(inputData)
                writeFile.Close()

            End If


Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,538
  • Joined: 12-December 12

Re: Create new line when writing to a txt file

Posted 03 February 2013 - 11:10 AM

FYI you can add a default to the InputBox:

inputData = InputBox("Enter what to save", "learninggoal")

Rather than hard-coding the path to your Documents folder you can construct it with:
sFileName = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) _ 
    & System.IO.Path.DirectorySeparatorChar & "somefile.txt"

where the path-separator on Windows is "\". This is a more flexible approach.

You can insert your subfolder as well:

sFileName = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) _ 
    & System.IO.Path.DirectorySeparatorChar & "Vb.netshiz" _
    & System.IO.Path.DirectorSeparatorChar & "somefile.txt"

This post has been edited by andrewsw: 03 February 2013 - 11:15 AM

Was This Post Helpful? 0
  • +
  • -

#9 LilGhost  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

Re: Create new line when writing to a txt file

Posted 03 February 2013 - 11:16 AM

that should work and what answersw said would work, but hard coding = better understanding in my opinion and not every language has the same functions, so it's useful to know different ways to write these functions for yourself.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,538
  • Joined: 12-December 12

Re: Create new line when writing to a txt file

Posted 03 February 2013 - 11:18 AM

Of course, it is easier to work with if you create variables:

Dim chPath = System.IO.Path.DirectorySeparatorChar

Was This Post Helpful? 0
  • +
  • -

#11 kcorj2244  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-January 13

Re: Create new line when writing to a txt file

Posted 03 February 2013 - 11:19 AM

View Postandrewsw, on 03 February 2013 - 11:10 AM, said:

FYI you can add a default to the InputBox:

inputData = InputBox("Enter what to save", "learninggoal")

Rather than hard-coding the path to your Documents folder you can construct it with:
sFileName = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) _ 
    & System.IO.Path.DirectorySeparatorChar & "somefile.txt"

where the path-separator on Windows is "\". This is a more flexible approach.

You can insert your subfolder as well:

sFileName = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) _ 
    & System.IO.Path.DirectorySeparatorChar & "Vb.netshiz" _
    & System.IO.Path.DirectorSeparatorChar & "somefile.txt"


Thanks! I left one of my questions in the code xD, so, how do I stop the rest of the code being executed after this If statement:

If TextBox1.Text = String.Empty Then
                FILE_NAME = "C:\Users\Ben\Documents\Vb.netshiz\learninggoal.txt"

                writeFile = IO.File.CreateText(FILE_NAME) 'Creates a new file
                writeFile.WriteLine(inputData)
                writeFile.Close()

            End If


Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,538
  • Joined: 12-December 12

Re: Create new line when writing to a txt file

Posted 03 February 2013 - 11:27 AM

If the code is in a sub-procedure then use Exit Sub (or Exit Function for a function).
Was This Post Helpful? 0
  • +
  • -

#13 LilGhost  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

Re: Create new line when writing to a txt file

Posted 03 February 2013 - 01:10 PM

i'd personally check it and say:
If Textbox1.Text = "" Then
 Msgbox("Fill in a save file name!")
Else
 ' Writing to File information
End If


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1