daveofgv
Well-known member
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):
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
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