Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Please find my code below,
DECLARE @DestLink ='D:\NewFolder\'
DECLARE @SrcLink ='C:\Test\Test1.docx'
Declare @strDateTimeToTicks BIGINT=0
SET @strDateTimeToTicks=(select dbo.DateTimeToTicks(getdate()))

DECLARE @extension VARCHAR(50)



SET @extension =(SELECT RIGHT(@SrcLink, CHARINDEX('.', REVERSE(@SrcLink))) AS [FileExtension])

SET @newPath='prefix'+ (Select CAST(@strDateTimeToTicks As varchar))+(Select CAST(@randomNumber As varchar))+@extension


Declare @newFlieName nvarchar(max)=''
SET @newFlieName=@DestLink--+@newPath




SET @SqlCopy = ' COPY /Y "' + @SrcLink + '" /B "' + @newFlieName

DECLARE @result int=0
EXEC master..xp_cmdshell @SqlCopy

My Question is it could not replace to destination filename
Posted
Updated 14-Oct-14 6:33am
v4
Comments
jaket-cp 13-Oct-14 12:31pm    
Not sure what you are asking.
Your code looks good, apart from missing double quote at end of @SqlCopy and to declare @SqlCopy.
Do you want to delete C:\New\Test1.docx after copying it to D:\New\Prefix123456.docx ?
24983 14-Oct-14 5:00am    
As per my my code, Now copied Test1.docx to another folder. It could not replace with Prefix123456.docx. I need to Replace ...Please help me.
jaket-cp 14-Oct-14 5:19am    
Not sure what you mean by replace?
Does D:\New\Prefix123456.docx exist already?
jaket-cp 14-Oct-14 8:15am    
Please update your question, there should be an improve question link or something.
Also when replying click on the Reply so people can be notified of the reply
jaket-cp 15-Oct-14 8:31am    
Have you managed to solve your issue?

Remove the + character. Your statement is trying to copy those two files to a new one, which will be named Test1.docx. You can check the exact syntax by typing copy /? in a command window.
 
Share this answer
 
Comments
Richard MacCutchan 14-Oct-14 6:02am    
I already told you what to do to correct it; please make an effort.
Having a quick look at your comment, @newPath is being commented out.
SQL
SET @newFlieName=@DestLink--+@newPath

The output of the command would be something like:
COPY /Y  "C:\New\Test1.docx"  /B "D:\New\"

This will copy the file Test1.docx in location C:\New\ to destination D:\New\ with the same file name Test1.docx.

Please read: http://ss64.com/nt/copy.html[^], I find this a good source for command line references.

Also you should be aware of the security issues when using xp_cmdshell.
http://blogs.msdn.com/b/sqlsecurity/archive/2008/01/10/xp-cmdshell.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900