Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-10-14
6
Medium Priority
?
498 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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 

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 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

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.
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 how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

704 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