Solved

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

Posted on 2014-10-14
6
403 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
  • 3
  • 2
6 Comments
 
LVL 65

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

816 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now