Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB.NET
Imports System.IO

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sourceFolderPath As String = "C:\Path\to\source\folder"
        Dim destinationFolderPath As String = "D:\Path\to\destination\folder"

        ' Create a folder with today's date
        Dim todayFolder As String = Path.Combine(destinationFolderPath, DateTime.Today.ToString("yyyy-MM-dd"))
        If Not Directory.Exists(todayFolder) Then
            Directory.CreateDirectory(todayFolder)
        End If

        ' Copy the source folder and its contents to the destination folder
        CopyFolder(sourceFolderPath, todayFolder)

        MessageBox.Show("Folder copied successfully!")
    End Sub

    Private Sub CopyFolder(sourceFolder As String, destinationFolder As String)
        ' Create destination folder if it doesn't exist
        If Not Directory.Exists(destinationFolder) Then
            Directory.CreateDirectory(destinationFolder)
        End If

        ' Copy files from source folder to destination folder
        For Each file As String In Directory.GetFiles(sourceFolder)
            Dim fileName As String = Path.GetFileName(file)
            Dim destinationFilePath As String = Path.Combine(destinationFolder, fileName)


           'error Line bellow

            File.Copy(file, destinationFilePath, True)
        Next

        ' Copy subfolders from source folder recursively
        For Each subfolder As String In Directory.GetDirectories(sourceFolder)
            Dim subfolderName As String = Path.GetFileName(subfolder)
            Dim destinationSubfolderPath As String = Path.Combine(destinationFolder, subfolderName)

     

            CopyFolder(subfolder, destinationSubfolderPath)

        Next
    End Sub
End Class


What I have tried:

error Line

VB
File.Copy(file, destinationFilePath, True)


visual studio2013 shows "Too many arguments to 'Public Shared Function Copy (str As String) As String"
Posted
Updated 17-May-23 21:30pm
v4

1 solution

Your For Each loop declares a variable called file - which the system assumes is what you mean when you write:
File.Copy(file, destinationFilePath, True)

Rename it and the confuseion will go away:
For Each filePath As String In Directory.GetFiles(sourceFolder)
    Dim fileName As String = Path.GetFileName(filePath)
    Dim destinationFilePath As String = Path.Combine(destinationFolder, fileName)
    File.Copy(filePath, destinationFilePath, True)
Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900