Search code examples
c#sql-serverssisetlscript-task

SSIS Copy files from one location to another


I want to copy a specific file from one location to another. I have a Message Box that prints where the file is copied source and destination as per below:

enter image description here

I get the below error:

enter image description here


Solution

  • Based on File.Copy Method, The second parameter is the new file name not the directory:

    The name of the destination file. This cannot be a directory or an existing file.

    You need to use a similar logic:

    FileCopy(filestocopy[p],targetDir + "\\" + Path.GetFileName(filestocopy[p]));
    

    Also it is recommended to check if the file already exists in the directory:

    if (!File.Exists(targetDir + "\\" + Path.GetFileName(filestocopy[p])))
        FileCopy(filestocopy[p],targetDir + "\\" + Path.GetFileName(filestocopy[p]));
    

    If you need to overwrite any existing file you can add a boolean parameter:

    FileCopy(filestocopy[p],targetDir + "\\" + Path.GetFileName(filestocopy[p]),true);