Below is a tiny program I modified from an example Microsoft code tutorial. This program simply copies .flac and .mp3 files from one specified folder to another, and if the file is already there it simply does not copy the file. I would like to make 2 changes to the program:
How would I go about making the error handling work (i.e. not crashing the program when an invalid input is made)?
Also, a big change I would like to make would be for it to copy the folder structure and all files in the original folder (At the moment, it only copies files that are directly in the original folder, not files that are in folders inside the original folder).
Thanks in advance.
How would I go about making the error handling work (i.e. not crashing the program when an invalid input is made)?
Also, a big change I would like to make would be for it to copy the folder structure and all files in the original folder (At the moment, it only copies files that are directly in the original folder, not files that are in folders inside the original folder).
VB.NET:
Imports System.IO
Module Module1
Sub Main()
Dim sourceDir As String
Dim backupDir As String
Console.WriteLine("Paste or type the location of the folder you want to copy from")
sourceDir = Console.ReadLine
Console.WriteLine("Paste or type the location of the folder you want to copy to")
backupDir = Console.ReadLine
Dim flacList As String() = Directory.GetFiles(sourceDir, "*.flac")
Dim mp3List As String() = Directory.GetFiles(sourceDir, "*.mp3")
Try
' Copy flac files.
For Each f As String In mp3List
'Remove path from the file name.
Dim fName As String = f.Substring(sourceDir.Length + 1)
Try
' Will not overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName))
' Catch exception if the file was already copied.
Catch copyError As IOException
Console.WriteLine(copyError.Message)
End Try
Next
' Copy mp3 files.
For Each f As String In mp3List
'Remove path from the file name.
Dim fName As String = f.Substring(sourceDir.Length + 1)
Try
' Will not overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName))
' Catch exception if the file was already copied.
Catch copyError As IOException
Console.WriteLine(copyError.Message)
End Try
Next
Catch dirNotFound As DirectoryNotFoundException
Console.WriteLine(dirNotFound.Message)
End Try
End Sub
End Module
Thanks in advance.