Solved

How do Copy, move, and rename a file in SSIS

Posted on 2014-10-14
6
467 Views
Last Modified: 2016-02-11
Good Morning,

I have a ssis package that I have created that basically moves data from a csv to a staging table then to a production table.  I want to automate the process.  

The file is downloaded to a directory

the file name is : GCPeclass_Course Report_2015_Semester_2014-10-12.csv and it is located \\ms01\courseinfo
the file name changes depending on the date.  ex.

eclass_Course Report_2015_Semester_2014-10-12.csv
eclass_Course Report_2015_Semester_2014_10_7.csv
eclass_Course Report_2015_Semester_2014-9-21.csv

when I get this file I want to  

copy the file

Move the original file to a folder called archive -\\ms01\courseinfo\archive

rename GCPeclass_Course Report_2015_Semester_2014-10-12 - Copy.csv
to GCPeclass_Course Report
0
Comment
Question by:Butterfly2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40380071
For starters, in the Control Flow add a File System Object (FSO) component, then double-click to edit the properties..
0
 

Author Comment

by:Butterfly2
ID: 40380091
Hi Jim,

I have gotten that far already.  I need assistance because the name changes(basically the last part of the file name is a date).
0
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 40380473
@Butterfly2

I don't think you'll be able to accomplish this with the File System Object (if u figure it out I'm interested in how you pulled it off).  

I usually end up using a "Script Task" and C# System.IO.File in order to pull this off.  It is additional coding though.

Maybe do something like this in your "script task":

 private static bool MoveAndOverWrite(string sSource, string sDestn)
        {
            try
            {
                if (System.IO.File.Exists(sSource) == true)
                {
                    System.IO.File.Copy(sSource, sDestn, true);

                    System.IO.File.Delete(sSource);

                    return true;                   
                }
                else
                {
                    Console.WriteLine("Specifed file does not exist");

                    return true;
                }
            }
            catch (System.IO.FileNotFoundException exFile)
            {
                Console.WriteLine("File Not Found " + exFile.Message);

                return false;
            }
            catch (System.IO.DirectoryNotFoundException exDir)
            {
                Console.WriteLine("Directory Not Found " + exDir.Message);

                return false;
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return false;
            }
        }

Open in new window


If you decide to go this route and need more direction, plz post a follow up question.  Thx
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Butterfly2
ID: 40380745
Hi Chris,

I am totally willing to try this way, but I am not sure exactly where your code goes. Does it go under expression?  If so what property do I choose?
0
 

Author Comment

by:Butterfly2
ID: 40380782
never mind i see where to put the code
0
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 500 total points
ID: 40380879
The code behind can be accessed by clicking the "Edit Script..." button.  

You'll need to pass your "source file name" and "destination directory" variables to the code behind by setting them in the "ReadOnlyVariables" property on that same page.

Once your on that code behind, I usually create global properties for these variables like this (example assumes variables are called myDestinationDirectory and myActiveFileName):

public string DestinationDirectory
{
            get
            {
                return Dts.Variables["myDestinationDirectory"].Value.ToString();
            }
}

Open in new window


and

public string ActiveFileName
{
            get
            {
                return Dts.Variables["myActiveFileName"].Value.ToString();
            }
}

Open in new window


Then you can just refer to these variables to make your code look a bit cleaner.

So moving forward, the entry point to the SSIS code behind is a void called Main().  Here we can call something similar to the function I posted earlier.

public void Main()
{
   string yourDateSuffix = "";  // replace with your suffix logic
   string newFileName = Path.GetFileName(ActiveFileName) + yourDateSuffix;
   MoveFile(ActiveFileName, DestinationDirectory, newFileName);
}

Open in new window


From this point we can leverage System.IO to do the work for us in a function like this:

private static bool MoveAndOverWrite(string sSource, string sDestnDir, string newFileName)
        {
            try
            {  
                string sDestn = sDestnDir + "\\" + newFileName;

                if (System.IO.File.Exists(sSource) == true)
                {

                    System.IO.File.Copy(sSource, sDestn, true);

                    System.IO.File.Delete(sSource);

                    return true;                   
                }
                else
                {
                    Console.WriteLine("Specifed file does not exist");

                    return true;
                }
            }
            catch (System.IO.FileNotFoundException exFile)
            {
                Console.WriteLine("File Not Found " + exFile.Message);

                return false;
            }
            catch (System.IO.DirectoryNotFoundException exDir)
            {
                Console.WriteLine("Directory Not Found " + exDir.Message);

                return false;
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return false;
            }
        }

Open in new window


Note: The code above probably has some syntax issues.  I wrote a lot of it in the browser window.  It should give you an idea as to how to accomplish this though.  If this looks like too much of a pain in the ass, no worries there may be an easier way to do it.  Good Luck.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question