Solved

Copy Backup Files

Posted on 2007-11-21
2
215 Views
Last Modified: 2010-03-19
I have a script to copy backup files.

This is what is does:

--Make a new directory to hold last month's backup files
SET @cmd = 'mkdir C:\dest\Archive\' + CONVERT(char(6), DATEADD(month, DATEDIFF(month, 0, getdate()) -1, 0), 112)
EXEC master..xp_cmdshell @cmd, NO_OUTPUT

--Loop through the filenames in the temp table and copy them to the new directory
DECLARE @current_dir varchar(255)

DECLARE test_cursor CURSOR FOR
SELECT
current_dir
FROM #test_archive

      
OPEN test_cursor
FETCH test_cursor INTO
@current_dir

WHILE (@@FETCH_STATUS = 0)
BEGIN


     SET @cmd = 'copy' +  @log_current_dir + 'C:\test\PLFullArchive' + CONVERT(char(6), DATEADD(month, DATEDIFF(month, 0, getdate()) -1, 0), 112)
      EXEC master..xp_cmdshell @cmd, NO_OUTPUT

      FETCH test_cursor INTO
      @current_dir
END



CLOSE test_cursor
DEALLOCATE test_cursor


The # test_archive temp table contains :

current_dir
C:\source\Archive\test_20071029_LOG_flip2.TRN
C:\source\Archive\test_20071029_LOG_flip2.TRN
C:\source\Archive\test_20071029_LOG_flip2.TRN
C:\source\Archive\test_20071029_LOG_flip2.TRN
C:\source\Archive\test_20071029_LOG_flip2.TRN
C:\source\Archive\test_20071029_LOG_flip2.TRN
C:\source\Archive\test_20071029_LOG_flip2.TRN

For some reason the copy command does not copy the files to the destination.
Is there something I am missing here.
This is part of a larger stored procedure.
You can run the copy command seperately.

Thanks


0
Comment
Question by:sbagireddi
2 Comments
 
LVL 17

Accepted Solution

by:
Chris Mangus earned 250 total points
ID: 20330529
Print the contents of the @cmd variable and examine it.  I don't think you have spaces where you need them.

i.e. - your output command probably reads:

copyc:\mylogdirC:\test\PLFullArchive...etc

Try:

SET @cmd = 'copy ' +  @log_current_dir + ' C:\test\PLFullArchive' + CONVERT(char(6), DATEADD(month, DATEDIFF(month, 0, getdate()) -1, 0), 112)
0
 
LVL 18

Assisted Solution

by:Yveau
Yveau earned 250 total points
ID: 20330572
Run the xp_cmdshell statement without the NO_OUTPUT and you will see what the error is.

My guess is:
'copy' +  @log_current_dir + 'C:\test\PLFullArchive' + CONVERT(char(6), DATEADD(month, DATEDIFF(month, 0, getdate()) -1, 0)

will end up like:
copyC:\source\Archive\test_20071029_LOG_flip2.TRNC:\test\PLFullArchive200710

and you mean to have something like:
copy C:\source\Archive\test_20071029_LOG_flip2.TRN C:\dest\Archive\200710

so you are looking for:
SET @cmd = 'copy ' +  @log_current_dir + ' C:\dest\Archive' + CONVERT(char(6), DATEADD(month, DATEDIFF(month, 0, getdate()) -1, 0), 112)

Hope this helps ...
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to import SQL 2000 database to SQL 2014 5 156
Query to Add Late Tolerance 10 82
Need to find substring in SQL 5 40
Can I skip a node in XML? 9 29
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question