Home Forums SQL Server 7,2000 General Copy and rename a file using stored procedure in SQL server 2005 RE: Copy and rename a file using stored procedure in SQL server 2005

  • paneri.rahul (7/22/2008)


    Hi

    I need to copy a timestamped file to a different folder and rename it(remove the timestamp) and finally delete the original file.

    Firstly, I need to copy the file located in the folder (C:\Webservices\datasource ) to (C:\Staging)

    The file is timestamped(TradeDetail04-02-08 00_00_00 .txt) and I need to remove the timestamp so that the filename becomes 'TradeDetail'.

    The original filename(with timestamp) changes daily with a new timestamp but the final file name remains the same ('Trade Detail')

    I tried simply renaming one of the files using xp_cmdshell:

    EXEC master..xp_cmdshell 'RENAME C:\Webservices\datasource\TradeDetail04-02-08 00_00_00 .txt TradeDetail.txt'

    but when I execute it there are two rows in the result:

    1) The syntax of the command is incorrect.

    2) Null

    Does anyone know how to accomplish this without using xp_cmdshell? Or may be the right syntax to do it?

    1. Since your filename: 'TradeDetail04-02-08 00_00_00.txt' has a space. The command does NOT recognize it, which will throw out an error.

    2. No idea about the result NULL. (since I tested success or failed, both gave me NULL.)

    For RENAME, it can only rename the file at the same folder. Why not use MOVE, which can move to different folder and rename the file at the same time. After that, you can delete the original one.