0

Is it even possible to access a file in a local drive, copy it, and place the copy in another location? If it is, can anyone show me how?

I am trying to create a job via the SQL Server Agent that copies a file and store it in another location.

1
  • I don't really understand what's being asked here. Copying files is the task of a file manager, not a SQL Server client. Commented Sep 17, 2020 at 20:35

1 Answer 1

2

Yes, it is possible by using the system procedure xp_cmdshell.

You can use any DOS commands using this procedure!

As an example you can use below command:

/* build copy command */  
SET @Cmd = 'COPY "C:\temp\1.bmp" "D:\1.bmp"';  
 
/* execute copy command */  
EXEC master.dbo.xp_cmdshell @Cmd; 

Note: Because of security risk, this procedure is disabled by default since a few version ago. You need to enable it, but note that this procedure is a security hole if your network is not secure. Also note that this is just a utility procedure, it might not be good idea to use it in applications except for very special scenarios. If you are going to use it in your application, you may need to rethink about your design.

To enable the xp_cmdshell use below commands:

-- To allow advanced options to be changed.  
EXECUTE sp_configure 'show advanced options', 1;  
GO  
-- To update the currently configured value for advanced options.  
RECONFIGURE;  
GO  
-- To enable the feature.  
EXECUTE sp_configure 'xp_cmdshell', 1;  
GO  
-- To update the currently configured value for this feature.  
RECONFIGURE;  
GO  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.