Reputation: 17
I have a GUI application (VB.NET) the user will search for file in a predefined directory and copy it to a predefined destination. For each file the user will search for there are two files with ALMOST the same name but different file size. 80% of the time this is the case, and 20% only one file available.
For instance, there are two files "12345-A" and "12345-B". What I want is when the user searches by using "12345" only, the application will compare A and B and copy the larger size and if there is only A or B just copy what's available. A & B are not constant - could be any letters.
I am not sure how I should start, but I have designed the GUI simply with a textbox to enter the file name (to search) and textBox for the new name and Start Copy button. I am using:
My.Computer.FileSystem.CopyFile(
string to copy,
destination,
FileIO.UIOption.OnlyErrorDialogs,
FileIO.UICancelOption.DoNothing
)
Any ideas?
Upvotes: 0
Views: 155
Reputation: 216293
The class DirectoryInfo allows you to retrieve the files in the source folder that match the specified pattern. The resulting array of FileInfo objects can be ordered by the Length property in descending order and finally the first one could be used as the source file for the copy.
Dim di = new DirectoryInfo("your_source_directory_with_files")
Dim sourceFile = di.GetFiles("12345*.*").
OrderByDescending(Function(x) x.Length).
FirstOrDefault()
if sourceFile IsNot Nothing Then
Dim destFile = Path.Combine("your_destination_directory", sourceFile.Name)
My.Computer.FileSystem.CopyFile(sourceFile.FullName, destFile,
FileIO.UIOption.OnlyErrorDialogs,
FileIO.UICancelOption.DoNothing)
End If
Upvotes: 1