Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9321385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:50:36+00:00 2026-06-19T03:50:36+00:00

I need to perform the following outlined task and am unsure how to proceed.

  • 0

I need to perform the following outlined task and am unsure how to proceed. Using Windows Server 2003, can I do this in a command script, or maybe a script task in SSIS? I have been using SSIS 2005 and know there is a file system task, but I’ve never used it before.

  • I have a file ‘target.file’ at local.
  • There is a ‘target’ folder on network server.
  • There are several hundred folders under ‘target’.
  • There are ‘backup’ folders under some of those folders.
  • I need to copy ‘target.file’ to those folders under the ‘target’ folder.
    • But only copy/replace if there is already a ‘target.file’ existing.
    • If the ‘target.file’ exists, copy and replace the file to the backup folder if the backup folder exists.
    • If not, create the backup folder first.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:50:37+00:00Added an answer on June 19, 2026 at 3:50 am

    Sample package written in SSIS 2012 using C# and VB.NET

    Here is a sample package written in SSIS 2012 that does what you are looking for using a script task. You don’t need to use SSIS. You can even do this with a simple C# or VB.NET console application, but SSIS gives the flexibility to log information and schedule the jobs.

    Folder structure (initial)

    Let’s assume that the folders are structured as shown below:

    There is a source file that you would like to copy.

    Source
        |- Sample_File.txt
    

    Here is the target folder structure.

    Target
        |- Target_1
        |    |- Archive
        |    |- Sample_File.txt
        |- Target_2
        |- Target_3
            |- Sample_File.txt
    

    Create an SSIS package and create the folder variables:

    Variable name       Data type  Value
    ------------------ ---------- ----------------------
    Backup_FolderName  String      Archive
    Source_FileName    String      Sample_File.txt
    Source_FilePath    String
    Source_Folder      String      D:\SSIS\Files\Source\
    Target_Folder      String      D:\SSIS\Files\Target\
    

    Select the variable Source_FilePath and click F4 to view the properties. Change the property EvaluateAsExpression to true. Click the ellipsis button next to the Expression property to open the Expression Builder. Set the expression to @[User::Source_Folder] + "\\" + @[User::Source_FileName].

    You could have just one variable to store the source file path. I usually prefer to keep the source folder and the file name separate.

    Drag and drop a script task onto the control flow tab. Double-click the script task to open the script task editor. On the script tab page, click the ellipsis button next to ReadOnlyVariables and select the following variables, because we will use these variables in the script task code.

    User::Source_FilePath
    User::Target_Folder
    User::Backup_FolderName
    

    Click the Edit Script… button and enter the code as shown below.

    Script Task code in C# only for SSIS 2008 and above:

    The script task code does the following:

    • It will check if the source file path is valid or not. If invalid, it will throw a message and quit the process.

    • It will check if the target folder is valid or not. If invalid, it will throw a message and quit the process.

    • If source file path and target folder are valid, the logic will loop through all the matching locations of the source file name in the sub-folders within target folder. If there are matching files, it will copy the target file to backup folder and then will overwrite the target file with source file.

    • The script task will emit the appropriate information so you can track the status within the progress/execution results tab on SQL Server Data Tools (SSDT) in SSIS 2012 or Business Intelligence Development Studio (BIDS) in SSIS 2005 – SSIS 2008 R2.

      region Namespaces

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

      endregion

      namespace ST_523853dfbc0d4123be43383671f8a6c6
      {
      [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
      public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
      {
      public void Main()
      {
      try
      {
      bool fireAgain = false;
      string backupFolder = string.Empty;
      string backupFilePath = string.Empty;

                  string sourcFilePath = Dts.Variables["User::Source_FilePath"].Value.ToString();
                  string targetFolder = Dts.Variables["User::Target_Folder"].Value.ToString();
                  string backupFolderName = Dts.Variables["User::Backup_FolderName"].Value.ToString();
      
                  if (String.IsNullOrEmpty(sourcFilePath) || !File.Exists(sourcFilePath))
                  {
                      // Check if a valid source file path was specified on the package variable
                      Dts.Events.FireError(101, "Source path error", String.Format("You need to set a valid source file path in the package variable 'Source_FilePath'. Invalid path: '{0}'", sourcFilePath), string.Empty, 0);
                      Dts.TaskResult = (int)ScriptResults.Failure;
                  }
                  else if (String.IsNullOrEmpty(targetFolder) || !Directory.Exists(targetFolder))
                  {
                      // Check if a valid target folder was specified on the package variable
                      Dts.Events.FireError(102, "Target folder error", String.Format("You need to set a valid target folder location in the package variable 'Target_Folder'. Invalid folder: '{0}'", targetFolder), string.Empty, 0);
                      Dts.TaskResult = (int)ScriptResults.Failure;
                  }
                  else
                  {
                      FileInfo sourceInfo = new FileInfo(sourcFilePath);
      
                      // Loop through each file that matches the name of the source file
                      foreach (string targetFilePath in Directory.EnumerateFiles(targetFolder, sourceInfo.Name, SearchOption.AllDirectories))
                      {
                          FileInfo targetInfo = new FileInfo(targetFilePath);
                          backupFolder = Path.Combine(targetInfo.Directory.FullName, backupFolderName);
                          backupFilePath = Path.Combine(backupFolder, backupFolderName);
      
                          // If the backup folder does not exist in the folder within root target folder, create the backup folder.
                          if (!Directory.Exists(backupFolder))
                          {
                              Directory.CreateDirectory(backupFolder);
                              Dts.Events.FireInformation(401, "Backup folder created", String.Format("Backup folder '{0}' was created.", backupFolder), string.Empty, 0, ref fireAgain);
                          }
      
                          // Archive the target file to the backup folder.
                          File.Copy(targetFilePath, backupFilePath, true);
                          Dts.Events.FireInformation(402, "Target file archived", String.Format("Target file '{0}' was archived to the backup folder '{1}'.", targetFilePath, backupFolder), string.Empty, 0, ref fireAgain);
      
                          // Overwrite the target file with the source file.
                          File.Copy(sourcFilePath, targetFilePath, true);
                          Dts.Events.FireInformation(403, "Target file overwritten", String.Format("Target file '{0}' was overwritten with the source file '{1}'.", sourcFilePath, targetFilePath), string.Empty, 0, ref fireAgain);
                      }
      
                      Dts.TaskResult = (int)ScriptResults.Success;
                  }
              }
              catch (Exception ex)
              {
                  Dts.Events.FireError(100, "Unhandled exception", ex.ToString(), string.Empty, 0);
              }
          }
      
          #region ScriptResults declaration
          enum ScriptResults
          {
              Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
              Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
          };
          #endregion
      }
      

      }

    Script Task code in VB.NET for SSIS 2005 and above:

    #Region "Imports"
    Imports System
    Imports System.Data
    Imports System.Math
    Imports System.IO
    Imports Microsoft.SqlServer.Dts.Runtime
    #End Region
    
    <Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()> _
    <System.CLSCompliantAttribute(False)> _
    Partial Public Class ScriptMain
        Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    
        Public Sub Main()
    
            Try
                Dim fireAgain As Boolean = False
                Dim backupFolder As String = String.Empty
                Dim backupFilePath As String = String.Empty
    
                Dim sourcFilePath As String = Dts.Variables("User::Source_FilePath").Value.ToString()
                Dim targetFolder As String = Dts.Variables("User::Target_Folder").Value.ToString()
                Dim backupFolderName As String = Dts.Variables("User::Backup_FolderName").Value.ToString()
    
                If String.IsNullOrEmpty(sourcFilePath) OrElse Not File.Exists(sourcFilePath) Then
                    ' Check if a valid source file path was specified on the package variable
                    Dts.Events.FireError(101, "Source path error", String.Format("You need to set a valid source file path in the package variable 'Source_FilePath'. Invalid path: '{0}'", sourcFilePath), String.Empty, 0)
                    Dts.TaskResult = ScriptResults.Failure
    
                ElseIf String.IsNullOrEmpty(targetFolder) OrElse Not Directory.Exists(targetFolder) Then
                    ' Check if a valid target folder was specified on the package variable
                    Dts.Events.FireError(102, "Target folder error", String.Format("You need to set a valid target folder location in the package variable 'Target_Folder'. Invalid folder: '{0}'", targetFolder), String.Empty, 0)
                    Dts.TaskResult = ScriptResults.Failure
    
                Else
                    Dim sourceInfo As FileInfo = New FileInfo(sourcFilePath)
    
                    ' Loop through each file that matches the name of the source file
                    For Each targetFilePath As String In Directory.EnumerateFiles(targetFolder, sourceInfo.Name, SearchOption.AllDirectories)
    
                        Dim targetInfo As FileInfo = New FileInfo(targetFilePath)
                        backupFolder = Path.Combine(targetInfo.Directory.FullName, backupFolderName)
                        backupFilePath = Path.Combine(backupFolder, backupFolderName)
    
                        ' If the backup folder does not exist in the folder within root target folder, create the backup folder.
                        If Not Directory.Exists(backupFolder) Then
                            Directory.CreateDirectory(backupFolder)
                            Dts.Events.FireInformation(401, "Backup folder created", String.Format("Backup folder '{0}' was created.", backupFolder), String.Empty, 0, fireAgain)
                        End If
    
                        ' Archive the target file to the backup folder.
                        File.Copy(targetFilePath, backupFilePath, True)
                        Dts.Events.FireInformation(402, "Target file archived", String.Format("Target file '{0}' was archived to the backup folder '{1}'.", targetFilePath, backupFolder), String.Empty, 0, fireAgain)
    
                        ' Overwrite the target file with the source file.
                        File.Copy(sourcFilePath, targetFilePath, True)
                        Dts.Events.FireInformation(403, "Target file overwritten", String.Format("Target file '{0}' was overwritten with the source file '{1}'.", sourcFilePath, targetFilePath), String.Empty, 0, fireAgain)
    
                    Next
    
                    Dts.TaskResult = ScriptResults.Success
    
                End If
    
            Catch ex As Exception
                Dts.Events.FireError(100, "Unhandled exception", ex.ToString(), String.Empty, 0)
                Dts.TaskResult = ScriptResults.Failure
            End Try
    
        End Sub
    
    #Region "ScriptResults declaration"
        Enum ScriptResults
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        End Enum
    
    #End Region
    
    End Class
    

    When an invalid source file path is provided, the package will throw the below error message:

    Source file error

    When an invalid target folder is provided, the package will throw the below error message:

    Target file error

    When source and target locations are valid, the package will execute successfully. In this example,

    • there was a backup folder under Target_1, so no folder was created but file was copied to backup folder.
    • There was no matching file in Target_2, so no action was taken.
    • Backup folder was created in Target_3, the file was copied to target location and then overwritten with source file.

    Successful

    Folder structure (Final)

    The target location will look like as shown below after the package execution.

    Target
        |- Target_1
        |    |- Archive
        |        |- Sample_File.txt
        |    |- Sample_File.txt
        |- Target_2
        |- Target_3
            |- Archive
                |- Sample_File.txt
            |- Sample_File.txt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to perform the following task : There are two 'ul' elements containing
I can perform the following SQL Server selection of distinct (or non-repeating names) from
Sometimes I need to perform following command cp -rv demo demo_bkp However I want
I need a method to perform the following task, suppose if I have a
We need to perform the following operation in our database : There is a
I need a query to perform the following: find the countryID where country =
I need a VBA function that will perform the following: Find all unique values
What I need - The code to perform the following: I am trying to
All, I need to write a regular expression to perform the following operations replace
I need to perform a find and replace using XSLT 1.0 which is really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.