Solved

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

Posted on 2014-10-14
6
399 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

910 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

18 Experts available now in Live!

Get 1:1 Help Now