SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
paneri.rahul
paneri.rahul
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 20
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?
Wildcat
Wildcat
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1273 Visits: 1444
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.
ADEDOYIN AJIBOLA
ADEDOYIN AJIBOLA
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 205
Hi all Im having exactly the same problem here does anyone know how to go about this?
paneri.rahul
paneri.rahul
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 20
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..
ADEDOYIN AJIBOLA
ADEDOYIN AJIBOLA
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 205
Hi Paneri,
Thanks very much for your timely reply. This is the perfect solution to the problem.

You are the star!
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)

Group: General Forum Members
Points: 218562 Visits: 41997
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
fguiccia
fguiccia
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 77
Thanks Paneri ! very handy! ;-)

Ferruccio Guicciardi
anilkumarg 33214
anilkumarg 33214
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 2
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

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)

Group: General Forum Members
Points: 218562 Visits: 41997
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50213 Visits: 10844
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



Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search