SQL Script To Copy text file from one location to other and Delete the File after Copying tO some log folder

  • Hi ,

    I need suggestions for following :

    I am trying to copy a text file from One location to other location and After copying file Need to Delete file from source location and need to keep a log of this file

  • I got the solution from one of my colleagues

    Please find the solution below

    DECLARE @from VARCHAR(200)

    DECLARE @to VARCHAR(200)

    DECLARE@to_move VARCHAR(200)

    DECLARE @sql VARCHAR(200)

    DECLARE @sql_mov VARCHAR(200)

    DECLARE @sql_del VARCHAR(200)

    SELECT @to = '\\X\20120724.txt',

    @from = '\\Y\20120724.txt',

    @to_move = '\\Z\20120724.txt'

    SET @sql = 'copy ' + @from + ' ' + @to

    SET@sql_mov = 'copy ' + @from + ' ' + @to_move

    SET@sql_del = 'del ' + @from

    EXECUTE master..xp_cmdshell @sql

    EXECUTE master..xp_cmdshell @sql_mov

    EXECUTE master..xp_cmdshell @sql_del

  • Rakesh.Chaudhary (7/24/2012)


    I got the solution from one of my colleagues

    Please find the solution below

    DECLARE @from VARCHAR(200)

    DECLARE @to VARCHAR(200)

    DECLARE@to_move VARCHAR(200)

    DECLARE @sql VARCHAR(200)

    DECLARE @sql_mov VARCHAR(200)

    DECLARE @sql_del VARCHAR(200)

    SELECT @to = '\\X\20120724.txt',

    @from = '\\Y\20120724.txt',

    @to_move = '\\Z\20120724.txt'

    SET @sql = 'copy ' + @from + ' ' + @to

    SET@sql_mov = 'copy ' + @from + ' ' + @to_move

    SET@sql_del = 'del ' + @from

    EXECUTE master..xp_cmdshell @sql

    EXECUTE master..xp_cmdshell @sql_mov

    EXECUTE master..xp_cmdshell @sql_del

    If you're going to use xp_CmdShell for this, there is a MOVE command in DOS and in ROBOCOPY.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks for the information about MOVE command. I was not aware about this earlier.

    I have now implemented my requirement using the MOVE command.

    Is there any other way to implement this other than using the xp_cmshell option?

  • Other option is to explore SSIS..

    ~ Lokesh Vij


    Guidelines for quicker answers on T-SQL question[/url]
    Guidelines for answers on Performance questions

    Link to my Blog Post --> www.SQLPathy.com[/url]

    Follow me @Twitter

  • Thanks!

    What should I use ,If want to Overwrite the existing file :

    Currently I am using below query ,but its not working....

    @sql = 'copy ' + @from + ' ' + @to +' /Y'

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply