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.
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.
Here is the target folder structure.
Create an SSIS package and create the folder variables:
Select the variable
Source_FilePathand clickF4to view the properties. Change the propertyEvaluateAsExpressionto true. Click the ellipsis button next to theExpressionproperty 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
ReadOnlyVariablesand select the following variables, because we will use these variables in the script task code.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;
}
Script Task code in VB.NET for SSIS 2005 and above:
When an invalid source file path is provided, the package will throw the below error message:
When an invalid target folder is provided, the package will throw the below error message:
When source and target locations are valid, the package will execute successfully. In this example,
Target_1, so no folder was created but file was copied to backup folder.Target_2, so no action was taken.Target_3, the file was copied to target location and then overwritten with source file.Folder structure (Final)
The target location will look like as shown below after the package execution.