You are on page 1of 8

SSIS: File System Task Move and rename files in one step

Posted by Rafael Salas


UPDATE: I have included a link to download the sample packae at the end of the post!
In some ET" scena#ios$ when p#ocessin files$ it is necessa#y to #ename the al#eady p#ocessed files and move them
to a diffe#ent location% In SSIS you can accomplish that in a sinle step usin the &ile System Task% The e'ample I
have p#epa#ed assumes the packae will p#ocess a set of files usin a &o#Each "oop containe#( then fo# each file$
usin the )Rename) ope#ation in &ile System Task will do both( #ename and move the file%
*e#e a#e some sc#een shots and notes about the packae:
&i#st of all$ the bi pictu#e% The cont#ol flow has a &o#Each "oop +ontaine# with a &ile System Task inside% ,otice that
the Data&low task is empty and it is intended to show whe#e the #eal ET" wo#k should o( but this can be diffe#ent o#
not #e-ui#ed at all%
Then details about the &o#Each "oop containe#% .asically $this containe# is confiu#ed to p#ocess all /%t't files in
+:0Temp0Sou#ce folde#$ whe#e all the files )to be p#ocessed) a#e e'pected to be%
,ow the t#ick$ few va#iables$ some of them usin e'p#essions:
The e'p#essions a#e:
in &ullSou#cePath&ile,ame:
@[User::SourcePath] + @[User::MyFileValue]
in &ullA#chivePath&ile,ame:
@[User::ArchivePath] + SUBSTRIN! @[User::MyFileValue] " # " FIN$STRIN! @[User::MyFileValue]"%&%"#' ( # ' +
%(% + !$T)STR" *" #*+*' Mo,th! @[Syste-::StartTi-e] '+ !$T)STR" ." #*+*' /ear! @[Syste-::StartTi-e] '+
SUBSTRIN! @[User::MyFileValue] " FIN$STRIN! @[User::MyFileValue]"%&%"#' " 01N! @[User::MyFileValue] ' '
,otice that Sou#cePath and A#chivePath va#iables hold only the o#iin and destination paths of the files%
,ote: 1ake su#e you set EvaluateAsE'p#ession p#ope#ty of the va#iable as TRUE%
"astly$ the &ile System Task should be confiu#ed like this:
I am p#etty su#e the#e a#e diffe#ent ways of accomplishin this simple task( but I like this one because it does not
#e-ui#e w#itin custom code and #elies on e'p#essions%
The File System task performs operations on fles and directories in the fle system. For example, by
using the File System task, a package can create, move, or delete directories and fles. You can also
use the File System task to set attributes on fles and directories. For example, the File System task can
make fles hidden or read-only.
ll File System task operations use a source, !hich can be a fle or a directory. For example, the fle
that the task copies or the directory it deletes is a source. The source can be specifed by using a File
connection manager that points to the directory or fle or by providing the name of a variable that
contains the source path. For more information, seeFile "onnection #anager and $ntegration Services
%ariables.
The operations that copy and move fle and directories and rename fles use a destination and a
source. The destination is specifed by using a File connection manager or a variable. File system task
operations can be confgured to permit over!riting of destination fles and directories. The operation
that creates a ne! directory can be confgured to use an existing directory that has the specifed name
instead of failing !hen the directory already exists.
-----
File connection manager enables a package to reference an existing fle or folder, or to create a fle
or folder at run time. Tasks and data &o! components in #icrosoft S'( Server $ntegration Services use
information in fles to perform their !ork by referencing a File connection manager that is included in
the package. For example, an )xecute S'( task can reference a fle that contains the S'( statements
that the task executes.
*hen you add a File connection manager to a package, $ntegration Services creates a connection
manager that !ill resolve to a File connection at run time, sets the File connection properties, and adds
the File connection to the "onnections collection of the package.
The "onnection#anagerType property of the connection manager is set to F$().
You can confgure a File connection manager in the follo!ing !ays+
Specify the usage type.
Specify a fle or folder.
Usage Types of the File Connection Manager
The File,sageType property of the File connection manager specifes ho! the fle connection is used.
The File connection manager can create a fle, create a folder, use an existing fle, or use an existing
folder.
n this blog post, !e !ill look into the S'( Server $ntegration Services -SS$S. feature provided by the
File System task, !hich is available in the control &o! tab as sho!n belo!+
s the name suggests, this task is used to perform operations on fles in the system-computer.. *ith
this task !e can move, delete folders and fles. *e can also use it to specify the attributes of fles.
The follo!ing activities can be performed !ith the help of File System Task+
"opy directory+ To copy all the fles from a folder to another directory. $n this case !e need to
confgure the source connection and destination folders.
"opy File+ To copy a single fle from one location to another.
"reate /irectory+ To create a folder, you need to specify the source connection !here the folder
is to be created -You need to have 0TFS rights for creation, if not rights run time error is
thro!n.
/elete /irectory+ To delete the specifed folder, you need to specify source connection. -you
need 0TFS rights for deletion.
/elete /irectory "ontent+ To delete all entries in a folder
/elete File+ To delete the specifed fle from the specifed location
#ove /irectory+ To move complete folder from one location to another
#ove fle+ To move the specifed fle from the specifed location to ne! one.
1ename fle+ To rename the specifed fle
Set ttributes+ to set the attributes for fles as !ell as folders, !hich can be hidden, readonly,
achieve and system.
(et us take a small example !here a variable stores the information about the destination folder.
2. "reate a ne! $ntegration Services 3ro4ect 5 rename the default package to
FileSystem/emo.dtsx
6. "reate 6 variables !ith package scope namely 7Source"onn8 and 7/estFolder8 of string type 5
specify the value as the re9uired folder names.
Note: To create a variable, select variables from the SS$S menu 5 select dd variable -ensure
that no task is selected so that the scope of the variable is for complete package.
:. /rag and drop the File System Task control on to the designer
;. "onfgure the File System Task in follo!ing manner
<. You !ill have to be careful !hile giving the source and connection values as variables, as the
re9uired folder and fles must be existing. =ther!ise !e !ill end up in getting a run time error.
>. To execute the package from ?usiness $ntelligence /evelopment Studio -?$/S. use F<.

You might also like