Copy image from one folder to another in vb.net

QuestionsCategory: VB.NETCopy image from one folder to another in vb.net
Shipra SinghShipra Singh Staff asked 1 year ago

I want to copy all image files from one folder to another folder programmatically using VB.net

1 Answers
Mahesh DeshmaneMahesh Deshmane answered 1 year ago

 
Below is Console program VB.NET Source code to move all images files from source folder to destination folder. This program simple move all files from source folder to destination folder.
If you want to move all image files which there is sub folder then we can add filter in GetFiles()
GetFiles(“*.*”, SearchOption.AllDirectories) – Includes the current directory and all its subdirectories in a search operation. This option includes reparse points such as mounted drives and symbolic links in the search.
GetFiles(“*.*”, SearchOption.TopDirectoryOnly) – Includes only the current directory in a search operation.

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)

        MovePngFiles(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

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

        Dim imageFileExtensions As String
        imageFileExtensions = "*.jpg,*.gif,*.png,*.bmp,*.jpe,*.jpeg,*.wmf,*.emf,*.xbm,*.ico,*.eps,*.tif,*.tiff,*.g01,*.g02,*.g03,*.g04,*.g05,*.g06,*.g07,*.g08"

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

        'Move all Files
        For Each file As FileInfo In sourcePath.GetFiles()
            If imageFileExtensions.Contains(file.Extension.ToLower()) Then
                file.MoveTo(Path.Combine(destinationPath.FullName, file.Name))
            End If
        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