File Copy Question

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
I have a program that copies files from one location to another. I place the file name in a spread sheet (column A) and my program will look at each row and if it cannot find the file it will create a missing log so I know which file is missing. If it can find the file than it copies the file to another location that I specify, however, the file as to be exact along with the extension (Below code):

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, 16) Is Nothing OrElse Len(Trim(sheet.Cells(row, 16).value)) = 0
        Do Until sheet.Cells(row, 1) Is Nothing OrElse Len(Trim(sheet.Cells(row, 1).value)) = 0
            Dim srce As String
            'Dim srce = Directory.GetFiles(txtSrce.Text, sheet.Cells(row, 15).value, SearchOption.AllDirectories).FirstOrDefault
            'Dim dest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 1).value)
            Dim dest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 1).value) ' & "_" & sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 3).value)



            ' original delete next row and uncomment this one Dim srcedest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 14).value & "_" & sheet.Cells(row, 15).value)
            Dim srcedest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 1).value) ' & "_" & sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 3).value)




            Try
                ' original delete next line and uncomment this one srce = Directory.GetFiles(txtSrce.Text, sheet.Cells(row, 15).value, SearchOption.AllDirectories).FirstOrDefault()
                srce = Directory.GetFiles(txtSrce.Text, sheet.Cells(row, 1).value, SearchOption.AllDirectories).FirstOrDefault()

                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)

                End If

                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, 15).value & vbCrLf)
                    Me.txtLog2.SaveFile(My.Settings.DestinationFolder & "\Duplicates_Log.txt", RichTextBoxStreamType.PlainText)
                Else


                    My.Computer.FileSystem.CopyFile(srce, dest)
                End If

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





            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)
                    My.Computer.FileSystem.RenameFile((dest), newName:=sheet.Cells(row, 1).value) ' & "_" & sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 3).value)

                End If

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



            row += 1


        Loop



        xls.Quit()
        xls = Nothing
        sheet = Nothing


        ProgressBar1.Value = ProgressBar1.Maximum
        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("Copy and Rename has completed" & vbNewLine & "number of files in the destination folder is: " & CStr(counter.Count))




        ' 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 Sub


My program works perfect, however, Problem is:

I now need to search for files that have more in the filename than I know of -

I need to find file = 123456789_helpdesk.xml
where I only know 123456789

does anyone know how to search for files that have the first 9 digits instead of the whole filename?
If there is already one available to download for free I can do that to. I have 1700 + files to find now.

Thanks in advanced.

daveofgv
 
The Directory.GetFiles method lets you specify a pattern, e.g. "123456789*". That will return an array containing the paths of all matching files. If the array is empty then there are no matches.
 
Thank you for the reply.....

With the above code - how would I specificly use Directory.GetFiles to look at the excel sheet and get all files with the name I specify in the column?

right now - it looks for exact matches.
 
How are the files recorded in the Excel sheet? Path, name and extension all in one column? Separate columns for all three? Some other arrangement?
 
I have it setup to copy all filenames from column A.

filename.ext
(123456789.tif)

VB.NET:
'browse for an Excel file containing the list of files to copy

        OpenFileDialog1.Title = "Select an Excel File"

        If txtExcel.Text <> "" Then
            OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.GetParentPath(txtExcel.Text)
        End If

        'OpenFileDialog1.Filter = "Excel Files(*.xls)|*.xls|Excel Files(*.xlsx)|*.xlsx"
        OpenFileDialog1.Filter = "Excel Files(*.xls;*.xlsx)|*.xls;*.xlsx"

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            txtExcel.Text = OpenFileDialog1.FileName
            SetCopyButtonText()
        End If
    End Sub

    Private Sub btnGetSrce_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetSrce.Click
        'browse for a folder containing the files to copy

        FolderBrowserDialog1.SelectedPath = txtSrce.Text

        If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
            txtSrce.Text = FolderBrowserDialog1.SelectedPath
            SetCopyButtonText()
        End If
    End Sub

    Private Sub btnGetDest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetDest.Click
        'browse for a folder that will contain the copied files
        'btnSave.Enabled = True
        FolderBrowserDialog2.SelectedPath = txtDest.Text

        If FolderBrowserDialog2.ShowDialog() = DialogResult.OK Then
            txtDest.Text = FolderBrowserDialog2.SelectedPath
            btnSave.Enabled = True
            SetCopyButtonText()
        End If
    End Sub
I select a path via OpenFileDialog and recurse through all the directories. Also, I have another OpenFileDialog that sets the dest path.
 
Ok so you're going to need to split the filename into name and extension so let's use ...

Dim FNParts() As String = Split(filename, ".")

then you use the getfiles function like this ...

file = My.Computer.FileSystem.GetFiles(directory, FileIO.SearchOption.SearchAllSubDirectories, FNParts(0) & "*." & FNParts(1)).FirstOrDefault

So that means we have a search pattern of 123456789*.ext which will find both files that have 9 letter (or fewer) names and the longer ones.
 
Thank you...

Not sure where to place

VB.NET:
file = My.Computer.FileSystem.GetFiles(directory, FileIO.SearchOption.SearchAllSubDirectories, FNParts(0) & "*." & FNParts(1)).FirstOrDefault

would it be possible to show me within the code above?

Thanks in advanced
 
I didn't study your code that hard but I assume the intention was to replace the line below? You'll obviously need to change the various 'default' variable names in my alternative to fit in with your program.


srce = Directory.GetFiles(txtSrce.Text, sheet.Cells(row, 1).value, SearchOption.AllDirectories).FirstOrDefault()
 
Thank you.

I now have

VB.NET:
Dim FNParts() As String = Split(filename, ".")

VB.NET:
srce = My.Computer.FileSystem.GetFiles(txtSrce.Text, sheet.Cells(row, 1).value, SearchOption.AllDirectories, FNParts(0) & "*." & FNParts(1)).FirstOrDefault

However, it says Split(filename, ".") is not delclared???
 
Er yeah, you need to replace 'filename' with the variable in which you're holding the filename as you read it from Excel (123456789.ext)
 
hummm... not working :(
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, 16) Is Nothing OrElse Len(Trim(sheet.Cells(row, 16).value)) = 0
        Dim FNParts() As String = Split(sheet.Cells(row, 1).value, ".")
        Do Until sheet.Cells(row, 1) Is Nothing OrElse Len(Trim(sheet.Cells(row, 1).value)) = 0
            Dim srce As String
            srce = My.Computer.FileSystem.GetFiles(txtSrce.Text, sheet.Cells(row, 1).value, FileIO.SearchOption.SearchAllSubDirectories, FNParts(0) & "*." & FNParts(1)).FirstOrDefault
            'Dim srce = Directory.GetFiles(txtSrce.Text, sheet.Cells(row, 15).value, SearchOption.AllDirectories).FirstOrDefault
            'Dim dest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 1).value)
            Dim dest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 1).value) ' & "_" & sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 3).value)



            ' original delete next row and uncomment this one Dim srcedest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 14).value & "_" & sheet.Cells(row, 15).value)
            Dim srcedest = My.Computer.FileSystem.CombinePath(txtDest.Text, sheet.Cells(row, 1).value) ' & "_" & sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 3).value)




            Try
                ' original delete next line and uncomment this one srce = Directory.GetFiles(txtSrce.Text, sheet.Cells(row, 15).value, SearchOption.AllDirectories).FirstOrDefault()
                '''''''' srce = Directory.GetFiles(txtSrce.Text, sheet.Cells(row, 1).value, SearchOption.AllDirectories).FirstOrDefault()


                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)

                End If

                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, 15).value & vbCrLf)
                    Me.txtLog2.SaveFile(My.Settings.DestinationFolder & "\Duplicates_Log.txt", RichTextBoxStreamType.PlainText)
                Else


                    My.Computer.FileSystem.CopyFile(srce, dest)
                End If

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





            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)
                    My.Computer.FileSystem.RenameFile((dest), newName:=sheet.Cells(row, 1).value) ' & "_" & sheet.Cells(row, 2).value & "_" & sheet.Cells(row, 3).value)

                End If

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



            row += 1


        Loop



        xls.Quit()
        xls = Nothing
        sheet = Nothing


        ProgressBar1.Value = ProgressBar1.Maximum
        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("Copy and Rename has completed" & vbNewLine & "number of files in the destination folder is: " & CStr(counter.Count))




        ' 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 Sub

any suggestions?
 
Here is the project if you don't mind looking into...

also, the code in question is button 22 :)
 

Attachments

  • New folder.zip
    128.6 KB · Views: 17
Last edited by a moderator:
Back
Top