Copy and rename a file using stored procedure in SQL server 2005

  • Hi

    Is there a way I can use T-SQL in SQL Server 2005 to accomplish this:

    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?

  • 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.

  • Hi all Im having exactly the same problem here does anyone know how to go about this?

  • Hi..

    May be this would help:

    First of all..make sure that the filename has no spaces

    then.. enable xp_cmdshell if its disabled using:

    EXEC sp_configure 'show advanced options', 1

    GO

    RECONFIGURE

    EXEC sp_configure 'xp_cmdshell', 1

    GO

    RECONFIGURE

    go

    then if u want to copy files.. use copy command:

    eg here I am copying all files starting with kss in the input folder to a file called kssdatasource in the datasource folder(/B is for binary,/Y for replace existing file):

    EXEC master..xp_cmdshell 'COPY /Y E:\Interface\Truck_Scale_Interface\Input\kss* /B E:\Interface\Truck_Scale_Interface\Datasource\kssdatasource.csv'

    If u want to Move files.. u can use 'Move' instead..

  • Hi Paneri,

    Thanks very much for your timely reply. This is the perfect solution to the problem.

    You are the star!

  • You CAN have file names with spaces (although, I prefer not to). And, you can easily run commands on those file names by enclosing the filename in double quotes... like this...

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

    --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 Paneri ! very handy! 😉

    Ferruccio Guicciardi

  • Jeff Moden - Saturday, August 16, 2008 7:57 PM

    You CAN have file names with spaces (although, I prefer not to). And, you can easily run commands on those file names by enclosing the filename in double quotes... like this...EXEC master..xp_cmdshell 'RENAME "C:\Webservices\datasource\TradeDetail04-02-08 00_00_00 .txt" TradeDetail.txt'

    Hi How do change the Date modified on a file using XP_CMDSHELL COPY Sourcefile to Destination File

  • anilkumarg 33214 - Friday, February 3, 2017 4:03 PM

    Jeff Moden - Saturday, August 16, 2008 7:57 PM

    You CAN have file names with spaces (although, I prefer not to). And, you can easily run commands on those file names by enclosing the filename in double quotes... like this...EXEC master..xp_cmdshell 'RENAME "C:\Webservices\datasource\TradeDetail04-02-08 00_00_00 .txt" TradeDetail.txt'

    Hi How do change the Date modified on a file using XP_CMDSHELL COPY Sourcefile to Destination File

    In general, the file would have to be modified and a COPY doesn't modify the file.  Neither does a RENAME  There is a way to do it but I've forgotten... it was almost 35 years ago when I did such a thing and haven't had to do the same since then.  Perhaps a visit to Yabingoolehoo would be worthwhile on that subject.

    --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)

  • Jeff Moden - Saturday, February 4, 2017 11:47 AM

    anilkumarg 33214 - Friday, February 3, 2017 4:03 PM

    Jeff Moden - Saturday, August 16, 2008 7:57 PM

    You CAN have file names with spaces (although, I prefer not to). And, you can easily run commands on those file names by enclosing the filename in double quotes... like this...EXEC master..xp_cmdshell 'RENAME "C:\Webservices\datasource\TradeDetail04-02-08 00_00_00 .txt" TradeDetail.txt'

    Hi How do change the Date modified on a file using XP_CMDSHELL COPY Sourcefile to Destination File

    In general, the file would have to be modified and a COPY doesn't modify the file.  Neither does a RENAME  There is a way to do it but I've forgotten... it was almost 35 years ago when I did such a thing and haven't had to do the same since then.  Perhaps a visit to Yabingoolehoo would be worthwhile on that subject.

    It's been a while, but I believe there's a Windows port of an old Unix command called touch.  When you touch a file, you update the last modified date and time.  IIRC, it's a simple copy/paste of the touch executable and then you can use it.  I've not tested it with any of the OSes past Windows XP or Windows 2000.

    HTH

  • Ed Wagner - Saturday, February 4, 2017 2:03 PM

    Jeff Moden - Saturday, February 4, 2017 11:47 AM

    anilkumarg 33214 - Friday, February 3, 2017 4:03 PM

    Jeff Moden - Saturday, August 16, 2008 7:57 PM

    You CAN have file names with spaces (although, I prefer not to). And, you can easily run commands on those file names by enclosing the filename in double quotes... like this...EXEC master..xp_cmdshell 'RENAME "C:\Webservices\datasource\TradeDetail04-02-08 00_00_00 .txt" TradeDetail.txt'

    Hi How do change the Date modified on a file using XP_CMDSHELL COPY Sourcefile to Destination File

    In general, the file would have to be modified and a COPY doesn't modify the file.  Neither does a RENAME  There is a way to do it but I've forgotten... it was almost 35 years ago when I did such a thing and haven't had to do the same since then.  Perhaps a visit to Yabingoolehoo would be worthwhile on that subject.

    It's been a while, but I believe there's a Windows port of an old Unix command called touch.  When you touch a file, you update the last modified date and time.  IIRC, it's a simple copy/paste of the touch executable and then you can use it.  I've not tested it with any of the OSes past Windows XP or Windows 2000.

    HTH

    Heh... or just use the date created, which will reflect when the copy was created and will be after the modified date.

    Personally, I feel dirty changing the modified date using something like "touch" or anything else because the file was, in fact, not modified.

    --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)

Viewing 11 posts - 1 through 10 (of 10 total)

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