not transferring the file

joemack95

Member
Joined
Nov 21, 2016
Messages
5
Programming Experience
Beginner
Basically i'm trying to get my program to copy a file from a location and send it over a series of ip addresses, i've commented each individual bit, but in short it just doesn't copy the file even tho it exists in the location


Public Class CopyPastFile


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

'this is only here so if you try to execute the copy command without a file selected it will alert you

If Textbox1.Text = "" Then
MsgBox("No file has been selected!")
Exit Sub
Else
End If


'this is creating the error log which will display wether the file was successfully copied to the machine or not
Dim path As String = "\\192.168.0.1\software\VB\errorlog.txt"
Dim fs As FileStream = File.Create(path)
Dim info As Byte() = New UTF8Encoding(True).GetBytes(urnstr & " " & ipstr)
fs.Write(info, 0, info.Length)
fs.Close()



' this goes through my database and selects each cell in a row and stores it tot he variable, which I then refer to
For rowIndex = 0 To DataGridView1.RowCount - 2
ipstr = DataGridView1.Rows(rowIndex).Cells(1).Value.ToString
urnstr = DataGridView1.Rows(rowIndex).Cells(0).Value.ToString
'MsgBox(urnstr & ipstr)


Try

'I think this is what is causing my error as I use a shell(xstr) command and it gives me a msgbox saying it was all good and my error log says successful but the file isn't transferred

with process.start it does the opposite and just says it cant find the file and the error log fails everything

xstr = ("xcopy " & fnstr & " " & ipstr & "\web /y")
Process.Start(xstr)

this writes all ip address as successful in the error log but only if the file is copied

My.Computer.FileSystem.WriteAllText("\\192.168.0.1\software\VB\errorlog.txt",
vbCrLf + urnstr & " " & ipstr & " " & " " & "Success", True)


Catch ex As Exception
MessageBox.Show("error ..." & ex.Message, "error")


'this does the opposite of the success and if it fails it lists it as a failure
My.Computer.FileSystem.WriteAllText("\\192.168.0.1\software\VB\errorlog.txt",
vbCrLf + urnstr & " " & ipstr & " " & " " & "Failed", True)



'this tells me if I am at the end of my datagrid view to cancel the loop
End Try
If rowIndex = DataGridView1.RowCount - 2 Then
Exit For
End If


Next
MessageBox.Show("Transfer Complete")


End Sub
 
Basically i'm trying to get my program to copy a file from a location and send it over a series of ip addresses, i've commented each individual bit, but in short it just doesn't copy the file even tho it exists in the location


Public Class CopyPastFile


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

'this is only here so if you try to execute the copy command without a file selected it will alert you

If Textbox1.Text = "" Then
MsgBox("No file has been selected!")
Exit Sub
Else
End If


'this is creating the error log which will display wether the file was successfully copied to the machine or not
Dim path As String = "\\192.168.0.1\software\VB\errorlog.txt"
Dim fs As FileStream = File.Create(path)
Dim info As Byte() = New UTF8Encoding(True).GetBytes(urnstr & " " & ipstr)
fs.Write(info, 0, info.Length)
fs.Close()



' this goes through my database and selects each cell in a row and stores it tot he variable, which I then refer to
For rowIndex = 0 To DataGridView1.RowCount - 2
ipstr = DataGridView1.Rows(rowIndex).Cells(1).Value.ToString
urnstr = DataGridView1.Rows(rowIndex).Cells(0).Value.ToString
'MsgBox(urnstr & ipstr)


Try

'I think this is what is causing my error as I use a shell(xstr) command and it gives me a msgbox saying it was all good and my error log says successful but the file isn't transferred

with process.start it does the opposite and just says it cant find the file and the error log fails everything

xstr = ("xcopy " & fnstr & " " & ipstr & "\web /y")
Process.Start(xstr)

this writes all ip address as successful in the error log but only if the file is copied

My.Computer.FileSystem.WriteAllText("\\192.168.0.1\software\VB\errorlog.txt",
vbCrLf + urnstr & " " & ipstr & " " & " " & "Success", True)


Catch ex As Exception
MessageBox.Show("error ..." & ex.Message, "error")


'this does the opposite of the success and if it fails it lists it as a failure
My.Computer.FileSystem.WriteAllText("\\192.168.0.1\software\VB\errorlog.txt",
vbCrLf + urnstr & " " & ipstr & " " & " " & "Failed", True)



'this tells me if I am at the end of my datagrid view to cancel the loop
End Try
If rowIndex = DataGridView1.RowCount - 2 Then
Exit For
End If


Next
MessageBox.Show("Transfer Complete")


End Sub
Your post is really hard to read with all of this unformatted code, try wrapping your code in [xcode=vb.net]Your code here[/xcode] tags, but I think your biggest issue is simply copying a file, have you taken a look at the System.IO.File.Copy() method?
 
Imports Microsoft.VisualBasic.FileIO
Imports System
Imports System.IO
Imports System.Text

Public Class CopyPastFile

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Textbox1.Text = "" Then
            MsgBox("No file has been selected!")
            Exit Sub
        Else
        End If

        Dim path As String = "\\192.168.0.1\software\VB\errorlog.txt"
        Dim fs As FileStream = File.Create(path)
        Dim info As Byte() = New UTF8Encoding(True).GetBytes(urnstr & " " & ipstr)
        fs.Write(info, 0, info.Length)
        fs.Close()

        For rowIndex = 0 To DataGridView1.RowCount - 2
            ipstr = DataGridView1.Rows(rowIndex).Cells(1).Value.ToString
            urnstr = DataGridView1.Rows(rowIndex).Cells(0).Value.ToString
            'MsgBox(urnstr & ipstr)

            Try
                xstr = ("xcopy " & fnstr & " " & ipstr & "\web /y")
                Process.Start(xstr)
                My.Computer.FileSystem.WriteAllText("\\192.168.0.1\software\VB\errorlog.txt", vbCrLf + urnstr & " " & ipstr & " " & " " & "Success", True)
            Catch ex As Exception
                MessageBox.Show("error ..." & ex.Message, "error")
                My.Computer.FileSystem.WriteAllText("\\192.168.0.1\software\VB\errorlog.txt", vbCrLf + urnstr & " " & ipstr & " " & " " & "Failed", True)
            End Try
            If rowIndex = DataGridView1.RowCount - 2 Then
                Exit For
            End If
        Next
        MessageBox.Show("Transfer Complete")

        For rowIndex = 0 To DataGridView1.RowCount - 2
            If My.Computer.FileSystem.FileExists(ipstr) Then
                MsgBox("File found.")
                Exit Sub
            Else

            End If
        Next
    End Sub
 
Last edited by a moderator:
not sure if that's easier without all my comments, and no I haent used that, I'm currently using the xcopy then executing it with process.start
 
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'Basically i'm trying to get my program to copy a file from a location and send it over a series of ip addresses, i've commented each individual bit, but in short it just doesn't copy the file even tho it exists in the location

 Public Class CopyPastFile
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'this is only here so if you try to execute the copy command without a file selected it will alert you

            If Textbox1.Text = "" Then
                MsgBox("No file has been selected!")
                Exit Sub
            Else
            End If

            'this is creating the error log which will display wether the file was successfully copied to the machine or not
            Dim path As String = "\\192.168.0.1\software\VB\errorlog.txt"
            Dim fs As FileStream = File.Create(path)
            Dim info As Byte() = New UTF8Encoding(True).GetBytes(urnstr & " " & ipstr)
            fs.Write(info, 0, info.Length)
            fs.Close()

            ' this goes through my database and selects each cell in a row and stores it tot he variable, which I then refer to
            For rowIndex = 0 To DataGridView1.RowCount - 2
                ipstr = DataGridView1.Rows(rowIndex).Cells(1).Value.ToStri ng
 urnstr = DataGridView1.Rows(rowIndex).Cells(0).Value.ToStri ng
 'MsgBox(urnstr & ipstr)

 Try
                    'I think this is what is causing my error as I use a shell(xstr) command and it gives me a msgbox saying it was all good and my error log says successful but the file isn't transferred
                    'With Process.Start it does the opposite And just says it cant find the file And the Error log fails everything

 xstr = ("xcopy " & fnstr & " " & ipstr & "\web /y")
                        Process.Start(xstr)

                       'this writes all ip address as successful in the error log but only if the file Is copied

 My.Computer.FileSystem.WriteAllText("\\192.168.0.1 \software\VB\errorlog.txt", vbCrLf + urnstr & " " & ipstr & " " & " " & "Success", True)

 Catch ex As Exception
                    MessageBox.Show("error ..." & ex.Message, "error")

                    'this does the opposite of the success and if it fails it lists it as a failure
                    My.Computer.FileSystem.WriteAllText("\\192.168.0.1 \software\VB\errorlog.txt",
                    vbCrLf + urnstr & " " & ipstr & " " & " " & "Failed", True)

'this tells me if I am at the end of my datagrid view to cancel the loop
                End Try
                If rowIndex = DataGridView1.RowCount - 2 Then
                    Exit For
                End If

            Next
            MessageBox.Show("Transfer Complete")

        End Sub
        End Sub
End Class
 
Last edited by a moderator:
Honestly you have so much jumbled code that none of that will ever work for you, so how about a good example of what you're trying to do here:
Option Explicit On
Option Strict On
Option Infer Off

Imports System.IO

Public Class Form1

    Private m_LogFilePath As String = "\\192.168.0.1\software\errorlog.txt"

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'Check if file has been selected
        If TextBox1.Text.Trim().Length > 0I Then

            'Variables to hold the two file names
            Dim ipstr, urnstr As String

            'Loop the DataGrid to copy the files
            For rowIndex As Integer = 0 To DataGridView1.RowCount - 2

                'Get the two file paths info
                ipstr = DataGridView1.Rows(rowIndex).Cells(1I).Value.ToString()
                urnstr = DataGridView1.Rows(rowIndex).Cells(0I).Value.ToString()

                'Check if the source file exists
                If File.Exists(ipstr) Then
                    Try
                        'Copy the file
                        File.Copy(ipstr, urnstr, True)

                        'Log successful file copy
                        WriteToLogFile(String.Format("{0} {1} Success", urnstr, ipstr))
                    Catch ex As Exception
                        'An error occured, log the info
                        WriteToLogFile(String.Format("{0} {1} Failed: {2}", urnstr, ipstr, ex.Message))
                    End Try
                End If
            Next rowIndex

            'Notify that the transfer completed
            MessageBox.Show("Transfer Complete", "Transfer Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            'Notify that there wasn't a file selected
            MessageBox.Show("No file has been selected!", "No File Selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

    Private Sub WriteToLogFile(Message As String)
        'Dim the file writer
        Dim sw As StreamWriter = Nothing

        Try
            'Create the file writer using the log file path, this will APPEND text to the file
            sw = New StreamWriter(m_LogFilePath, True)

            'Write the data line
            sw.WriteLine(Message)
        Catch ex As Exception
            'There was an error writing to the log file
            MessageBox.Show(ex.ToString(), "Error writing to log file", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            'Close & clean up the file writer
            If sw IsNot Nothing Then
                sw.Close()
                sw.Dispose()
            End If
        End Try
    End Sub

End Class
 

Latest posts

Back
Top