Need help copy and rename from subfolders - I can't find one little peice

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
Hello all -

I have a program that is pretty much done, however, one little piece is missing. I have an excel sheet with two columns. One column is the name of a file and the other column is a name that will be added to the filename once the file has been copied.

Example:
Column B = test.tif
Column A = find

The result will be = find_test.tif once the file has been copied and renamed......

All is good, however, it appears my program is not looking in subfolders and only in the main directory folder for the images.

What I have so far for the copy portion:

VB.NET:
'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 = 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)

            ' Dim dest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value)
            ' If My.Computer.FileSystem.FileExists(dest) Then


            'If System.IO.File.Exists(srce) = False Then
            '    txtlog.AppendText(sheet.Cells(row, 1).value & vbCrLf)
            'ElseIf System.IO.File.Exists(dest) = True Then
            'txtLog2.AppendText(sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value & vbCrLf)

            ' ElseIf My.Computer.FileSystem.FileExists(srce) Then
            '  End If


            If System.IO.File.Exists(srce) = False Then
                txtlog.AppendText(sheet.Cells(row, 1).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, 2).value & "_" & sheet.Cells(row, 1).value & vbCrLf)
                    Me.txtLog2.SaveFile(My.Settings.DestinationFolder & "\Duplicates_Log.txt", RichTextBoxStreamType.PlainText)
                Else
                    My.Computer.FileSystem.CopyFile(srce, dest)
                End If
            End If

            Try
                If System.IO.File.Exists(srcedest) = False Then
                    'If System.IO.File.Exists(srcedest) = False Then
                    My.Computer.FileSystem.RenameFile((dest), newName:=sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value)
                    'Else
                    ' If System.IO.File.Exists(srcedest) = True Then
                    'txtLog2.AppendText(sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value & vbCrLf)
                End If

                'End If

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

            '    Else
            'Try
            '    My.Computer.FileSystem.RenameFile((dest), newName:=sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value)
            'Catch ex As Exception
            '    txtLog2.AppendText(sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value)
            'End Try
            '    End If








            'If System.IO.File.Exists(srce) = False Then
            '    txtlog.AppendText(sheet.Cells(row, 1).value & vbCrLf)

            'test try uncomment the below lines with ''''
            '''' If System.IO.File.Exists(dest) = True Then
            'If My.Computer.FileSystem.FileExists(dest) Then
            ''''txtLog2.AppendText(sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value & vbCrLf)
            '''' Else
            '''' My.Computer.FileSystem.RenameFile((dest), newName:=sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value)
            ''''    End If
            ' End If

            'If My.Computer.FileSystem.FileExists(dest) Then
            '    txtLog2.AppendText(sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value & vbCrLf)
            'Else
            '    My.Computer.FileSystem.RenameFile((dest), newName:=sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value)
            'End If
            'If My.Computer.FileSystem.FileExists(dest) Then
            '    txtLog2.AppendText(sheet.Cells(row, 1).value & vbCrLf)

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










            'My.Computer.FileSystem.RenameFile(srce, dest)
            '*******************************************************
            ' My.Computer.FileSystem.RenameFile(srce, dest)
            '*******************************************************
            '*Else : txtlog.AppendText(sheet.Cells(row, 1).value & vbCrLf)
            '*End If
            '********************************************************************************
            'Else : txtLog.AppendText(sheet.Cells(row, 1).value & vbCrLf)
            'If My.Computer.FileSystem.FileExists(dest) Then
            '    txtLog2.AppendText(sheet.Cells(row, 1).value & vbCrLf)
            'End If

            'Else
            '    txtLog.AppendText(sheet.Cells(row, 1).value & vbCrLf)
            '**********************************************************************************************
            'If the excel will  be column A file name and column B new name use below (play around until figured out
            '' '' '' '' '' ''If My.Computer.FileSystem.FileExists(dest) Then
            '' '' '' '' '' ''    txtLog2.AppendText(sheet.Cells(row, 2).value & vbCrLf)
            '' '' '' '' '' ''ElseIf My.Computer.FileSystem.FileExists(srce) Then
            'If My.Computer.FileSystem.FileExists(dest) Then
            'txtLog2.AppendText(sheet.Cells(row, 1).value & vbCrLf)

            'txtLog2.AppendText(sheet.Cells(row, 1).value & vbCrLf)
            'If My.Computer.FileSystem.FileExists(dest) Then
            '    txtLog2.AppendText(sheet.Cells(row, 2).value & vbCrLf)
            '    txtLog2.AppendText(sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 1).value & vbCrLf)
            'Else

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

            'End If

            '' '' '' '' ''Else : txtlog.AppendText(sheet.Cells(row, 2).value & vbCrLf)
            '' '' '' '' ''End If
            '**********************************************************************************************
            'Else : txtlog.AppendText(sheet.Cells(row, 1).value & vbCrLf)
            'End If

            'txtLog2.AppendText(sheet.Cells(row, 1).value & vbCrLf)

            ''End If
            ''''''''ElseIf My.Computer.FileSystem.FileExists(srce) Then
            'Else




            'End If
            'txtLog.AppendText("Missing Files" & vbCrLf & sheet.Cells(row, 1).value & vbCrLf)
            'If My.Computer.FileSystem.FileExists(dest) = False Then
            'txtLog2.AppendText(sheet.Cells(row, 1).value & vbCrLf)
            'End If
            'txtLog.AppendText("copy" & vbTab & srce & vbCrLf)
            '  txtLog.AppendText("  to" & vbTab & dest & vbCrLf)

            row += 1

        Loop



        ' xls = Nothing

        xls.Quit()
        xls = Nothing
        sheet = Nothing

        MessageBox.Show("Copy and Rename has completed")
End Sub

What do I need to change in the above code for it to look in subfolders as well as the main folder?

This is probably very simple and I am just missing somthing.

Thanks in advacned

daveofgv
 
Thanks for the reply JohnH -

So, would I be correct for the above code to be:

VB.NET:
My.Computer.FileSystem.GetFiles(srce, dest)

instead of:
VB.NET:
My.Computer.FileSystem.CopyFile(srce, dest)
 
Back
Top