Tuesday, April 21, 2009

Copy/Rename a file using File System Task in SSIS

This is a reference answer to a post on MSDN forum but is very useful

Question: While copying and renaming a file to a dynamic location through File System Task using variable throws an error

Error: Failed to lock variable "c:\test\test_200904202009.txt" for read access with error 0xC0010001 "The variable cannot be found. This occurs when an attempt is made to retrieve a variable from the Variables collection on a container during execution of the package, and the variable is not there. The variable name may have changed or the variable is not being created.".
OR

How to copy and rename a file to a dynamic location using SSIS?

Solution:

I will move and rename a file "test.txt" from c:\ to c:\backup with new name appended with date.

1) Create three variables

Src_File = c:\test.txt
Dest_File = test
Dest_Dir = c:\backup


2) In connection Manager create a File Connection with name as DestinationConn

3) In Expression property of DestinationConn use ConnectionString and provide following expression

@[Dest_Dir] + "\\" + @[Dest_File] + "_" + (DT_WSTR,4)DatePart("yyyy", GetDate()) + RIGHT("0" + (DT_WSTR,2)DatePart("mm", GetDate()), 2) + RIGHT("0" + (DT_WSTR,2)DatePart("dd", GetDate()), 2) + RIGHT("0" + (DT_WSTR,2)DatePart("hh", GetDate()), 2) + RIGHT("0" + (DT_WSTR,2)DatePart("mi", GetDate()), 2) + ".txt"



this will set the destination path and new name for the file

4) Create a File System task and configure like :




5) Execute the package

To run the package create a file named "test.txt"  and a folder "backup"at C:\

Thanks!!
Please leave a comment.

54 comments:

  1. Im getting following error:-

    The error is thrown by Connections collection when the specific connection element is not found

    ReplyDelete
  2. hi

    need help
    In above expression we get getdate as file name . But in my case i need one day previous date from Tusday to Friday and on Monday 3 days previous date .

    Could you plz help .

    Thanks

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. Thanks is was exactly what I needed

    ReplyDelete
  5. Thanks is was exactly what I needed too

    ReplyDelete
  6. Thnxx a lottt..!!

    ReplyDelete
  7. Very good it works

    ReplyDelete
  8. Thank you so much!!!!!!!! This is what exactly I am looking for and it worked for me. :)

    ReplyDelete
  9. I've been trying to do similar but with several files. What's the set up on the ForEach Loop Container? Can it be done?

    ReplyDelete
  10. Thank you!!! I've just lost 1/2 day trying to do the same thing in a wrong way!

    ReplyDelete
  11. Hi , I have got a error stating destination connection manager DestinationConn has an invalid usage type "Folder Exists" KINDLY HELP ME

    ReplyDelete
  12. Well done... thank you!

    ReplyDelete
  13. This is awesome. thanks!

    ReplyDelete
  14. problems copying or renaming files

    website:LongPathTool.com - unlock and delete path too long files

    When you are having problems with deleting, copying or renaming files
    on your computer, you should get an effective tool. You can find
    Windows tool to delete or copy folders and files with errors such as
    path too long or the filename too long. Basically search the file and
    then press the button to delete or copy it. Path too long application
    solves problems with the capacity to delete or copy files and folders
    with the long paths in Explorer. At times, errors are created by
    software programs, virus, adware and more.

    ReplyDelete
  15. Works like a charm, thanks a lot!

    ReplyDelete
  16. You are really kind genius

    ReplyDelete
  17. I like this trick. This trick is good for users who love to play with technical ways. However, I have used “Long Path Tool” in this situation and it helped me easily and quickly. So this tool is best for those members who don't love to play with technical skills.

    ReplyDelete
  18. This is a great post. Very simple to follow and it works! Love it!

    ReplyDelete
  19. Can we move a file into a folder then rename it appending date with the file name?

    ReplyDelete
  20. Good One Man.Carry on with your great work

    ReplyDelete
  21. Finally! An example of a rename and copy that works! Thank you.

    ReplyDelete
  22. perfect, thanks !

    ReplyDelete
  23. Worked like a charm!!! Thanks

    ReplyDelete
  24. Yes this method really works on my blog. I am thankful to your article and suggesting also to other friends to have a look on it.

    ReplyDelete
  25. I cant down load this package, what do i have to do?

    ReplyDelete
  26. Thank you very much for this great tool. It work very well when you run it in Visual Studio.
    Unfortunately, when I try to run teh package from a SQL Job, I got the following error :

    Error Code: 0xC0012024 Source: TimeStamp & Backup Description: The task "TimeStamp & Backup" cannot run on installed (64-bit) of Integration Services. It requires Standard Edition (64-bit) or higher. DTExec: The package execution returned DTSER_FAILURE

    Any idea why ? Thank you again for your work.

    ReplyDelete
  27. Sweet! Thanks. I was struggling with trying to set expressions on the File System Task and not the Connection Manager.

    ReplyDelete
  28. Problem: This performs a move & rename not a copy & rename .....

    Solution: change operation to copy file

    ReplyDelete
  29. Hello..

    We have same situation.

    Long Path Tool helped me. http://PathTooDeep.com

    You can visit and check out. Thank You!

    ReplyDelete
  30. Good Job! Exactly what I'm looking for. Thank you!

    ReplyDelete
  31. Thanks a lot, it solved a lot of issues!
    Walter

    ReplyDelete
  32. Thanks for posting your posting, it really helped me to solve my requirement.

    Bhandary

    ReplyDelete
  33. How can I get it to copy and rename. When I try this it is moving and renaming.

    ReplyDelete
  34. I use a software for this" Long Path Too"l and it really works for deleting copying and renaming files or folder.

    ReplyDelete
  35. Very Nice! Excellent post and precisely what I needed without a lot of grief or endless searching on the WWW. Thank You Sir!

    Bob

    ReplyDelete
  36. Just what I needed. Thanks!

    ReplyDelete
  37. Hi Buddy,

    It executes only once , if we want to rerun the task its not executing ,is there any alternative to process it.

    ReplyDelete
  38. Hi

    I suggest to try "Long Path Tool" program .

    before I have problem cannot copy file because file name too long. and after I found long

    path tool and now my problem solved

    Thanks

    Patrick

    ReplyDelete
  39. the maximum ... thanks

    ReplyDelete
  40. It is usefull software for copy, delete, long path files.
    Try "Long Path Tool" program.

    ReplyDelete
  41. I would recommend in this case to try program Long Path Tool

    ReplyDelete
  42. very helpful. thank you

    ReplyDelete
  43. i need to do this for some files is possible ?

    ReplyDelete
  44. Exactly what I needed! Perfect solution for single file archiving and date-stamping, when looping through a directory is NOT required! Thanks Rahul - you're a stud brother!

    ReplyDelete
  45. many many thx Rahul
    exactly what I was looking for and documented in a way that I was able to use it without any experience with SSIS.

    ReplyDelete
  46. I would recommend you to try Long Path Tool program to fix this problem

    ReplyDelete
  47. Worked great for me. Thanks!

    ReplyDelete
  48. Hello There,

    What you’re saying is absolutely correct Copy/Rename a file using File System Task in SSIS, but this isn’t the exact situation everywhere. Where most smart folk work on a project - why can’t you do this the Boss asks :).

    I decided I had better get all the spec's for my dell latitude d600 and post them to see what linix os I can use. I use my laptop for personal and business use for my Ham Radio events, such as skywarn and emergency communications.

    But great job man, do keep posted with the new updates.

    Cheers,
    Preethi.

    ReplyDelete

Google Dataset Search

Google's Vision statement  is “ to provide access to the world's information in one click. ” Google’s mission Statement is “ ...