Copy a CSV file to another CSV File with vb.net

Good Morning! Hope all is well in your part of the world.

I myself was informed yesterday that we would be moving to another building at GE Healthcare. Of course there will not be a cafeteria or coffee shop (we have been so spoiled) so we are all kind of bummed about that. But other than that I am fine.

So what I needed to do was copy a CSV file to an empty CSV file. Not overly complicated but it was something that needed to be done. Hope it don’t bore you. Make it a great day!

Public Class Form1
    Private WithEvents tmr As New Timer
    Private sr As System.IO.StreamReader
    Private sw As System.IO.StreamWriter
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tmr.Interval = 1000
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False
        TextBox1.Enabled = False
        TextBox2.Enabled = False
        Try           
            sr = New System.IO.StreamReader(TextBox1.Text)
            sw = New System.IO.StreamWriter(TextBox2.Text, False)
            tmr.Start()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Button1.Enabled = True
            TextBox1.Enabled = True
            TextBox2.Enabled = True
        End Try
    End Sub
    Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
        If (Not sr Is Nothing) AndAlso (Not sw Is Nothing) Then
            Try
                Dim line As String = sr.ReadLine
                If Not line Is Nothing Then
                    sw.WriteLine(line)
                    Debug.Print(line)
                Else
                    tmr.Stop()
                    sr.Close()
                    sw.Close()
                    Button1.Enabled = True
                    TextBox1.Enabled = True
                    TextBox2.Enabled = True
                End If
            Catch ex As Exception
                tmr.Stop()
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                If Not sr Is Nothing Then
                    sr.Close()
                End If
                If Not sw Is Nothing Then
                    sw.Close()
                End If
                Button1.Enabled = True
                TextBox1.Enabled = True
                TextBox2.Enabled = True
            End Try
        End If
    End Sub
End Class

 

 

Windows Live Tags: vb.net,.NET,csv,copy,streamwriter,timer

 

Advertisements
  1. #1 by Frank on August 15, 2008 - 7:25 pm

    You ARE spoiled! 🙂
     
    We have 4-5 coffee machines and 2 cappucino makers we bought ourselves in the whole building, for probably about 200 people. 
     
    Any reason for the move?

  2. #2 by Frank on August 15, 2008 - 7:29 pm

    Oh also, I don\’t have a lot of 1.1 background, so I\’m not sure if the static function below is available, but couldn\’t you just do a
     
    System.IO.File.Copy(source, destination, true) ?
     
    Its an empty destination, but isn\’t the above equivalent?

  3. #3 by Kelly on August 16, 2008 - 9:12 am

    No reason for the move so far. We think it may have to do with the cluster our parking situation is. But they are moving 245 people. We were use to having two cafeterias and a coffee shop that is like Starbucks so yes we are spoiled.
     
    🙂

  4. #4 by Kelly on August 16, 2008 - 9:13 am

    well what i didnt put in which I probably shoudl have is that we are only moving certain rows from the csv file. But I eleminated the testing part to see cause it was very simple.

  5. #5 by Frank on August 16, 2008 - 11:12 am

    Understood, yeah the File.Copy(…) method doesn\’t let you test or filter your data.
     
     

  6. #6 by George on August 26, 2008 - 6:42 pm

    This is good stuff. I\’m coming from 14 years or so of Lotus Notes development and this .net stuff, while familiar, is different enough from VB6 like development (Lotusscript is just like VB3-6) to give me a headache.  Sample code like this is invaluable.  Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: