Excel Copy and Rename Error

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
I have posted several times about this copy and rename program I am creating for work.

A new issue that I have come across is that when the program reads my excel sheet and uses the text in a cell (suppose to be file name) it looks for it, however, the file name is: Heaven gated community \#23.tif

When the program see's the "\" it errors out and say Unhandled Expression cannot find the path Heaven gated community \#23.tif...........

All the other files with spaces it finds, however, the unsupported characters cause a problem is seems like.

I added a Try Catch in my copy section and wonder if someone can look at my code and let me know if I have it correct to catch these errors.

VB.NET:
txtlog.Text = ""
        txtLog2.Text = ""

        If RadioButton1.Checked = True Then

            ProgressBar1.Minimum = 0

            ProgressBar1.Maximum = 5

            ProgressBar1.Value = 0


            If ProgressBar1.Value < ProgressBar1.Maximum Then

                ProgressBar1.Value += 1

                'If ProgressBar1.Value = ProgressBar1.Maximum Then

                '    MsgBox("Finished!")

                'End If

            End If





            'copy the files specified in column 1 of the first sheet of the Excel workbook
            'from the folder given by txtSrce to the folder given by txtDest

            Dim xls As New Excel.Application
            Dim sheet As Excel.Worksheet

            xls.Workbooks.Open(txtExcel.Text)
            sheet = xls.ActiveWorkbook.Sheets(1)

            Dim row As Integer = 1
            Do Until sheet.Cells(row, 1) Is Nothing OrElse Len(Trim(sheet.Cells(row, 1).value)) = 0


                Dim srce = Directory.GetFiles(txtSrce.Text, sheet.Cells(row, 1).value, SearchOption.AllDirectories).FirstOrDefault
                'Dim dest = Directory.GetFiles(txtDest.Text, sheet.Cells(row, 2).value, SearchOption.AllDirectories).FirstOrDefault
                Dim dest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 2).value)



                ' Dim srce = My.Computer.FileSystem.CombinePath(txtSrce.Text, sheet.Cells(row, 1).value)
                ' Dim dest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 2).value)
                Dim srcedest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value)





                If System.IO.File.Exists(srce) = False Then
                    txtlog.AppendText(sheet.Cells(row, 2).value & vbCrLf)
                    Me.txtlog.SaveFile(My.Settings.DestinationFolder & "\Missing_Log.txt", RichTextBoxStreamType.PlainText)
                Else
                    If System.IO.File.Exists(srce) = True AndAlso System.IO.File.Exists(srcedest) = True Then
                        txtLog2.AppendText(sheet.Cells(row, 1).value & "_" & sheet.Cells(row, 2).value & vbCrLf)
                        Me.txtLog2.SaveFile(My.Settings.DestinationFolder & "\Duplicates_Log.txt", RichTextBoxStreamType.PlainText)
                    Else
                        Try
                            My.Computer.FileSystem.CopyFile(srce, dest)
                        Catch ex As Exception
                            txtLog3.AppendText(sheet.Cells(row, 14).value & "_" & sheet.Cells(row, 15).value & vbCrLf)
                        End Try
                        'My.Computer.FileSystem.GetFiles(srce)

                        'System.IO.File.Copy(srce, SearchOption.AllDirectories, dest)
                        'My.Computer.FileSystem.GetFiles(srce, SearchOption.AllDirectories, dest)




                    End If
                End If

                Try
                    If System.IO.File.Exists(srcedest) = False Then

                        My.Computer.FileSystem.RenameFile((dest), newName:=sheet.Cells(row, 1).value & "_" & sheet.Cells(row, 2).value)

                    End If


                Catch ex As Exception
                    'txtLog2.AppendText(sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value & vbCrLf)
                End Try



                row += 1

            Loop





            xls.Quit()
            xls = Nothing
            sheet = Nothing
            ProgressBar1.Value = ProgressBar1.Maximum
            MessageBox.Show("Copy and Rename has completed")
            Dim desten = txtDest.Text
            Dim counter As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
            counter = My.Computer.FileSystem.GetFiles(desten)



            Dim result1 As DialogResult = MessageBox.Show("number of files in the destination folder is: " & CStr(counter.Count))
            If result1 = DialogResult.OK Then
                ProgressBar1.Value = 0
            End If


        ElseIf RadioButton2.Checked = True Then


            ProgressBar1.Minimum = 0

            ProgressBar1.Maximum = 5

            ProgressBar1.Value = 0


            If ProgressBar1.Value < ProgressBar1.Maximum Then

                ProgressBar1.Value += 1

                'If ProgressBar1.Value = ProgressBar1.Maximum Then

                '    MsgBox("Finished!")

                'End If

            End If





            'copy the files specified in column 15 of the first sheet of the Excel workbook
            'from the folder given by txtSrce to the folder given by txtDest

            Dim xls As New Excel.Application
            Dim sheet As Excel.Worksheet

            xls.Workbooks.Open(txtExcel.Text)
            sheet = xls.ActiveWorkbook.Sheets(1)

            Dim row As Integer = 1
            Do Until sheet.Cells(row, 15) Is Nothing OrElse Len(Trim(sheet.Cells(row, 15).value)) = 0


                Dim srce = Directory.GetFiles(txtSrce.Text, sheet.Cells(row, 15).value, SearchOption.AllDirectories).FirstOrDefault
                'Dim dest = Directory.GetFiles(txtDest.Text, sheet.Cells(row, 2).value, SearchOption.AllDirectories).FirstOrDefault
                Dim dest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 15).value)



                ' Dim srce = My.Computer.FileSystem.CombinePath(txtSrce.Text, sheet.Cells(row, 1).value)
                ' Dim dest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 2).value)
                Dim srcedest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 14).value & "_" & sheet.Cells(row, 15).value)





                If System.IO.File.Exists(srce) = False Then
                    txtlog.AppendText(sheet.Cells(row, 15).value & vbCrLf)
                    Me.txtlog.SaveFile(My.Settings.DestinationFolder & "\Missing_Log.txt", RichTextBoxStreamType.PlainText)

                Else

                    If System.IO.File.Exists(srce) = True AndAlso System.IO.File.Exists(srcedest) = True Then
                        txtLog2.AppendText(sheet.Cells(row, 14).value & "_" & sheet.Cells(row, 15).value & vbCrLf)
                        Me.txtLog2.SaveFile(My.Settings.DestinationFolder & "\Duplicates_Log.txt", RichTextBoxStreamType.PlainText)
                    Else


                        'My.Computer.FileSystem.GetFiles(srce)
                        My.Computer.FileSystem.CopyFile(srce, dest)

                            Try
                            My.Computer.FileSystem.CopyFile(srce, dest)
                        Catch ex As Exception
                            txtLog3.AppendText(sheet.Cells(row, 14).value & "_" & sheet.Cells(row, 15).value & vbCrLf)
                        End Try

                        'System.IO.File.Copy(srce, SearchOption.AllDirectories, dest)
                        'My.Computer.FileSystem.GetFiles(srce, SearchOption.AllDirectories, dest)


                    End If
                End If



                Try
                    If System.IO.File.Exists(srcedest) = False Then

                        My.Computer.FileSystem.RenameFile((dest), newName:=sheet.Cells(row, 14).value & "_" & sheet.Cells(row, 15).value)

                    End If


                Catch ex As Exception
                    'txtLog2.AppendText(sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value & vbCrLf)
                End Try



                row += 1


            Loop



            xls.Quit()
            xls = Nothing
            sheet = Nothing


            ProgressBar1.Value = ProgressBar1.Maximum
            MessageBox.Show("Copy and Rename has completed")
            Dim desten = txtDest.Text
            Dim counter As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
            counter = My.Computer.FileSystem.GetFiles(desten)


            Dim result2 As DialogResult = MessageBox.Show("number of files in the destination folder is: " & CStr(counter.Count))
            If result2 = DialogResult.OK Then
                ProgressBar1.Value = 0
            End If
        End If
    End Sub

The part I added to hopefully catch these errors are:

VB.NET:
    Try
                            My.Computer.FileSystem.CopyFile(srce, dest)
                        Catch ex As Exception
                            txtLog3.AppendText(sheet.Cells(row, 14).value & "_" & sheet.Cells(row, 15).value & vbCrLf)
                        End Try
                        'My.Computer.FileSystem.GetFiles(srce)

Anyone - does this seem correct?

Thanks in advanced

daveofgv
 
Back
Top