Error Handling & Copying Folder Structure

IceQubed

New member
Joined
May 2, 2011
Messages
2
Programming Experience
Beginner
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).

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.
 
I always used recursion in this situation but if you move or copy the directory you will get all the contents otherwise you will need to use recursion:

VB.NET:
    Public DestDir As New System.IO.DirectoryInfo("c:\newpath")
    Sub Main()
        Dim dir As New System.IO.DirectoryInfo("c:\rootpath")
        ProcessAllFilesAndFolders(dir)
        'ProcessAllFolders(dir)
    End Sub
    Sub ProcessAllFilesAndFolders(ByVal dir As System.IO.DirectoryInfo)
        For Each fi As System.IO.FileInfo In dir.GetFiles
            'loop through all files in this folder
            'swap out from root path to new path and take our filename
            Dim newfilepath As String = fi.FullName.Replace("rootpath", "newpath")
            If My.Computer.FileSystem.FileExists(newfilepath) Then
                'the file already exists do what you need
            Else
                Try
                    fi.MoveTo(newfilepath)
                Catch ex As Exception
                    'if the move does not work you end up here
                End Try
            End If
        Next
        For Each d As System.IO.DirectoryInfo In dir.GetDirectories
            'loop through all folders recursively to go all the way down through the tree
            ProcessAllFilesAndFolders(d)
        Next
    End Sub
    Sub ProcessAllFolders(ByVal dir As System.IO.DirectoryInfo)
        For Each d As System.IO.DirectoryInfo In dir.GetDirectories
            Try
                'move folder and contents
                System.IO.Directory.Move(d.FullName, d.FullName.Replace("rootpath", "newpath"))
            Catch ex As Exception
                'move failed
            End Try
        Next
    End Sub
 
Back
Top