vb.net move all files from one folder to another

QuestionsCategory: VB.NETvb.net move all files from one folder to another
Shipra SinghShipra Singh Staff asked 1 year ago

How to move all files from one folder to another folder using VB.NET?

1 Answers
Mahesh DeshmaneMahesh Deshmane answered 1 year ago

Below is Console program VB.NET Source code to move all files and folders from source folder to destination folder. This program simple move all files from source folder to destination folder.
 
 

Imports System.IO

Module FileMover

    Sub Main()
        Dim sourceFolderPath As String
        Dim destinationFolderPath As String

        Console.WriteLine("Enter Source Folder Path")
        sourceFolderPath = Console.ReadLine()

        Console.WriteLine("Enter Source Folder Path")
        destinationFolderPath = Console.ReadLine()

        Dim sourceDirectory = New DirectoryInfo(sourceFolderPath)
        Dim destinationDirectory = New DirectoryInfo(destinationFolderPath)

        Move(sourceDirectory, destinationDirectory)
        Console.WriteLine("Done")
    End Sub

    Public Sub Move(sourcePath As DirectoryInfo, destinationPath As DirectoryInfo)

        If (Not destinationPath.Exists) Then
            destinationPath.Create()
        End If

        'Move all Files
        For Each file As FileInfo In sourcePath.GetFiles()
            file.MoveTo(Path.Combine(destinationPath.FullName, file.Name))
        Next

        'Move all folders
        For Each dir As DirectoryInfo In sourcePath.GetDirectories()
            dir.MoveTo(Path.Combine(destinationPath.FullName, dir.Name))
        Next
    End Sub

End Module