July 22, 2008 at 7:33 am
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?
July 22, 2008 at 12:48 pm
paneri.rahul (7/22/2008)
HiI 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.
August 14, 2008 at 8:35 am
Hi all Im having exactly the same problem here does anyone know how to go about this?
August 14, 2008 at 11:47 am
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..
August 16, 2008 at 5:07 pm
Hi Paneri,
Thanks very much for your timely reply. This is the perfect solution to the problem.
You are the star!
August 16, 2008 at 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'
--Jeff Moden
Change is inevitable... Change for the better is not.
October 28, 2010 at 9:55 am
Thanks Paneri ! very handy! 😉
Ferruccio Guicciardi
February 3, 2017 at 4:03 pm
Jeff Moden - Saturday, August 16, 2008 7:57 PMYou 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
February 4, 2017 at 11:47 am
anilkumarg 33214 - Friday, February 3, 2017 4:03 PMJeff Moden - Saturday, August 16, 2008 7:57 PMYou 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
Change is inevitable... Change for the better is not.
February 4, 2017 at 2:03 pm
Jeff Moden - Saturday, February 4, 2017 11:47 AManilkumarg 33214 - Friday, February 3, 2017 4:03 PMJeff Moden - Saturday, August 16, 2008 7:57 PMYou 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
February 4, 2017 at 3:41 pm
Ed Wagner - Saturday, February 4, 2017 2:03 PMJeff Moden - Saturday, February 4, 2017 11:47 AManilkumarg 33214 - Friday, February 3, 2017 4:03 PMJeff Moden - Saturday, August 16, 2008 7:57 PMYou 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
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply