Move all files in a folder
Hello all,
I am trying to move all files in a folder to a different folder with a SSIS package. So I created a foreach loop container, which has a user variable that supposed to hold the name of each file. The flat file connection manager for the source has this variable as
the connectionstring in expression.
I have added a file system task inside the foreach container. The file system task supposed to move the files to the destination. So for the file system task, I have set issourcepathvariable true and the variable I have given is the user variable of foreach
contanier.
But when I execute the package it gives the error that user variable is empty. Please let me know how it can be fixed.
Thanks.
May 27th, 2011 1:01pm
May be you did not map it?
See variable mapping part in my blog: http://geekswithblogs.net/Compudicted/archive/2011/05/17/how-to-ftp-and-process-anyhow-named-file.aspx
Just in case, use this blog article to x-ref with your solution:
http://www.rafael-salas.com/2007/03/ssis-file-system-task-move-and-rename.htmlArthur My Blog
Free Windows Admin Tool Kit Click here and download it now
May 27th, 2011 1:14pm
Thanks for the links. For the variable, I can see that the index value is set at 0. How can I make it 1 (like your example)? Is this the cause of the problem?
May 27th, 2011 1:24pm
0 is fine.Arthur My Blog
Free Windows Admin Tool Kit Click here and download it now
May 27th, 2011 1:31pm
Instead of using for each and then file system task it is simpler to use just a script task.
i think it might help you.try these:
Variables var = null;
String src, des;
FileInfo source, dest;
Dts.VariableDispenser.LockForRead("User::directory_path");
Dts.VariableDispenser.LockForRead("User::destination_path");
Dts.VariableDispenser.GetVariables(ref var);
src = (String)var["User::directory_path"].Value;
des = (String)var["User::destination_path"].Value;
string[] files= System.IO.Directory.GetFiles (src);
foreach (string filename in files)
{
source = new FileInfo(filename);
dest = new FileInfo(des + source.Name);
if (dest.Exists)
dest.Delete();
source.MoveTo(dest.FullName);
}
Dts.TaskResult = (int)ScriptResults.Success;
}
latha
May 27th, 2011 1:48pm
Here is an other example:
http://microsoft-ssis.blogspot.com/2011/02/how-to-configure-foreach-loop-file.html
Don't forget to map the variable in the loop. 0 is the first item (zero based).
Please mark the post as answered if it answers your question | My SSIS Blog:
http://microsoft-ssis.blogspot.com
Free Windows Admin Tool Kit Click here and download it now
May 27th, 2011 1:51pm
Yes I did that. Also for the file system task I have given the variable as source variable. But there is a 'x' mark on the file system task (which is inside the foreach container) and the error message is that the variable that is used as source is empty.
Any thoughts?
May 27th, 2011 2:42pm
Another option is to use a "Execute Process Task' to run some power-shell or a command line command.
Free Windows Admin Tool Kit Click here and download it now
May 27th, 2011 2:51pm
Go to the properties of File System Task and make the Delay Validation "True".
If you dont want to do so then,
Please assign the file path to your variable and dont leave it empty.
May 27th, 2011 2:52pm
Assign an initial value to variable you have an issue with in the variable properties and all should work!Arthur My Blog
Free Windows Admin Tool Kit Click here and download it now
May 27th, 2011 2:54pm
Yes I did that. Also for the file system task I have given the variable as source variable. But there is a 'x' mark on the file system task (which is inside the foreach container) and the error message is that the variable that is used as source
is empty. Any thoughts?
Fill the variable with a default path or set Delay Validation to True like nplus suggested.
(Sorry for the propose as anwer, hit the wrong button)Please mark the post as answered if it answers your question | My SSIS Blog:
http://microsoft-ssis.blogspot.com
May 27th, 2011 2:55pm