473,423 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,423 software developers and data experts.

How to copy a file with timestamp using xp_cmdshell,

filename to be copied,the source path and destination path are provided as input from .net app.When I copy file using xp_cmdshell copy /y sourcepath destinationpath the file is copied but the destination filename is wierd.For example my source file looks like fpdir.txt.[2010-07-09] and i want it to be copied as fpdir.tx to destination path.but the output filename appears as fpdir.txt.[2010-07-09]fpdir.txt

here is the sp
DECLARE @SourceFile VARCHAR(1000)
DECLARE @DestFile VARCHAR(1000)

DECLARE @Cmd VARCHAR(2000)
DECLARE @Flag VARCHAR(1)

IF LEN(@FileName) > 0
BEGIN
SET @SourceFile = @SourcePath + @FileName
SET @DestFile = @DestinationPath + RTRIM(LTRIM(SUBSTRING(@FileName,1,CHARINDEX('[',@FileName,0)-2)))
SET @Cmd = 'COPY /Y "' + @SourceFile + '" "' + @DestFile + '"'

END

EXEC XP_CMDSHELL @Cmd
SET @SourceFile = ''
please let me know the solution
Jul 2 '10 #1
3 3607
Jerry Winston
145 Expert 100+
That's a very peculiar result for this code. I copied and completed(a few declarations and SET's were missing) your code and got the proper result. The only problem I could immediately identify was the line:
Expand|Select|Wrap|Line Numbers
  1. SET @DestFile = @DestinationPath + RTRIM(LTRIM(SUBSTRING(@FileName,1,CHARINDEX('[',@FileName,0)-2)))
Which should have had (-3):
Expand|Select|Wrap|Line Numbers
  1. SET @DestFile = @DestinationPath + RTRIM(LTRIM(SUBSTRING(@FileName,1,CHARINDEX('[',@FileName,0)-3)))
Here's the code:

Expand|Select|Wrap|Line Numbers
  1. DECLARE @FileName varchar(1000)
  2. DECLARE @SourceFile VARCHAR(1000)
  3. DECLARE @DestFile VARCHAR(1000)
  4. DECLARE @SourcePath varchar(1000)
  5. DECLARE @DestinationPath varchar(1000)
  6.  
  7. DECLARE @Cmd VARCHAR(2000)
  8. DECLARE @Flag VARCHAR(1)
  9. SET @FileName = 'fpdir.txt.[2010-07-09]'
  10. SET @SourcePath = 'X:\tmp\'
  11. SET @Cmd = ''
  12. SET @Flag = ''
  13. SET @DestinationPath = 'T:\newFol\'
  14. SET @DestFile = ''
  15. SET @SourceFile = ''
  16.  
  17. IF LEN(@FileName) > 0
  18. BEGIN
  19.  
  20. SET @SourceFile = @SourcePath + @FileName
  21.  
  22. SET @DestFile = @DestinationPath + RTRIM(LTRIM(SUBSTRING(@FileName,1,CHARINDEX('[',@FileName,0)-3)))
  23.  
  24. SET @Cmd = 'COPY /Y "' + @SourceFile + '" "' + @DestFile + '"' 
  25.  
  26. SELECT @Cmd
  27. END 
  28.  
Results:
Expand|Select|Wrap|Line Numbers
  1. COPY /Y "X:\tmp\fpdir.txt.[2010-07-09]" "T:\newFol\fpdir.tx"
Jul 2 '10 #2
@b0010100
thanks for the response. The code is working fine.this sp was called from UI and the parameters passed where wrong it works fine...:-)
Jul 5 '10 #3
Jerry Winston
145 Expert 100+
Glad you got it working!
Jul 11 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Terri | last post by:
I'm using xp_cmdshell to output a text file from a trigger like this CREATE TRIGGER ON tblApplications FOR INSERT AS DECLARE @FirstName varchar(75) DECLARE @LastName varchar(75) Declare...
4
by: Joel Thornton | last post by:
Whenever something is inserted to a given table, I want to run some shell commands using xp_cmdshell. Would it be a bad idea to put this xp_cmdshell in the INSERT trigger of this table? I...
1
by: JimM | last post by:
is it possible to change file timestamp on ftp server using ftp command? Thanks
2
by: Paul | last post by:
Dear All, I want to use web form to upload my file and copy the file to another machine. I can upload the file, but when I copy the file(file.CopyTo(".....", true)) to another machine(map...
9
by: MAF | last post by:
Does anyone know why in 2005 I might be getting this error everytime I try and recompile? Error 226 Unable to copy file "obj\Debug\myfile.dll" to "bin\Debug\myfile.dll". The process cannot...
3
by: MAF | last post by:
Does anyone know why in 2005 I might be getting this error everytime I try and recompile? Error 226 Unable to copy file "obj\Debug\myfile.dll" to "bin\Debug\myfile.dll". The process cannot...
5
by: Standist | last post by:
I want install software on PC in my local network. First I want copy file to the PC through default share like c$ or d$ on the PC. Then install the software through wmi. I have the administrator...
0
by: Pivalig | last post by:
Hello, i try to copy file from Pocket PC to network printer using this code : destination = "\\\\pc1\\ep1000\\Bill.txt"; File.Copy("\\Program Files\\MobileKhelner\\Bill.txt", destination); ...
9
rizwan6feb
by: rizwan6feb | last post by:
Hello everybody. I need help on how to use copy function to copy file from one computer to another ( or any other way i can copy the file). The computers are on a local network.
1
by: Irrigho5 | last post by:
i have the open file function just need help with the copy file function and main Ask for a source filename and a target filename. Then copy the source file to the target file using the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.