Monday, May 28, 2012

Script To Move File From One Folder To Other (C#)


/*
   Microsoft SQL Server Integration Services Script Task
   Write scripts using Microsoft Visual C# 2008.
   The ScriptMain is the entry point class of the script.
*/

using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Xml;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;

namespace ST_782e82416722494db4d051a43d3603d6.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

        /*
        The execution engine calls this method when the task executes.
        To access the object model, use the Dts property. Connections, variables, events,
        and logging features are available as members of the Dts property as shown in the following examples.

        To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
        To post a log entry, call Dts.Log("This is my log text", 999, null);
        To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);

        To use the connections collection use something like the following:
        ConnectionManager cm = Dts.Connections.Add("OLEDB");
        cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";

        Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
       
        To open Help, press F1.
    */

        public void Main()
        {
            string SourceFileName = string.Empty;
            string SourceFileFolder = string.Empty;
            string SourceFilePath = string.Empty;
            string ArchiveFileFolderName = string.Empty;
            string RejectFilePath = string.Empty;
            string ArchiveFileName = string.Empty;
            bool   ErrorFlag = false;
            string ErrorMessage = "";
            //--string LoadStatus = "";

            System.DateTime vG_ProcessEndDate = System.DateTime.MinValue;

            SourceFileFolder = Dts.Variables["vG_SourceFileFolder"].Value.ToString();
            SourceFileName = Dts.Variables["vG_SourceFileName"].Value.ToString();
            ArchiveFileFolderName = Dts.Variables["vG_ArchiveFileFolder"].Value.ToString();
            SourceFilePath = SourceFileFolder + SourceFileName;
            ArchiveFileName = ArchiveFileFolderName + SourceFileName;
            try
            {
                if (File_Move(SourceFilePath, ArchiveFileName) == true)
                {
                    ErrorFlag = false;
                    ErrorMessage = "No Error";

                }
                else
                {
                    ErrorFlag = true;
                }

                Dts.Variables["vG_ProcessedFileFolder"].Value = ArchiveFileFolderName;
                Dts.Variables["vG_ProcessedFilePath"].Value = ArchiveFileName;
                Dts.Variables["vG_ProcessedFileName"].Value = SourceFileName;
                Dts.Variables["vG_LoadStatus"].Value = "Sucess";
                Dts.Variables["vG_ProcessEndDate"].Value = DateTime.Now;
                Dts.Variables["vG_ErrorMessage"].Value = ErrorMessage;
            }
            catch (Exception ex)
            {
                Dts.Variables["vG_ProcessedFileFolder"].Value = ArchiveFileFolderName;
                Dts.Variables["vG_ProcessedFilePath"].Value = ArchiveFileName;
                Dts.Variables["vG_ProcessedFileName"].Value = SourceFileName;
                Dts.Variables["vG_LoadStatus"].Value = "Error in File Archive";
                Dts.Variables["vG_ProcessEndDate"].Value = DateTime.Now;
                Dts.Variables["vG_ErrorMessage"].Value = ex.Message;
            }
            // TODO: Add your code here
            Dts.TaskResult = (int)ScriptResults.Success;
        }

        public bool File_Move(string OldFileName, string NewFileName)
        {
            try
            {
                string sPath = System.IO.Path.GetDirectoryName(NewFileName);
                if (!Directory.Exists(sPath)) Directory.CreateDirectory(sPath);

                if (File.Exists(NewFileName) == false)
                {
                    File.Move(OldFileName, NewFileName);
                    return true;
                }
                {
                    return false;
                }               
            }
            catch (Exception ex)
            {
                bool bVal = false;
                Dts.Events.FireInformation(0, "Write to Log", "Error during file move: " + ex.Message, "", 0, ref bVal);
                return false;
            }
        }

        /*
   Microsoft SQL Server Integration Services Script Task
   Write scripts using Microsoft Visual C# 2008.
   The ScriptMain is the entry point class of the script.
   Date         Author      Description
   07-May-10    Sam K       Created Initial Version of the Program
  
*/

    }
}

No comments:

Post a Comment