Solved

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

Posted on 2014-10-14
6
384 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Lessons learned during ten years of interviewing for SQL Server Integration Services (SSIS) and other Extract-Transform-Load (ETL) contract roles and two years of staff manager interviewing contractors.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

743 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

15 Experts available now in Live!

Get 1:1 Help Now