question

DeepikaShinde avatar image
DeepikaShinde asked

Move files from one folder to another

I am trying to move file from D:\try\ t1.txt to folder D:\trycopy\ EXEC master..xp_cmdshell 'MOVE /Y D:\try\ t1.txt /B D:\trycopy\' But its not moved. Error "The system cannot find the file specified." t1.txt file is there on the respective file.
xp_cmdshell
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

I am executing statement on server and trying to access file from my drive.. Plz help
0 Likes 0 ·
ASK SQL Server Central runs by you voting. For all helpful answers below, please indicate this by clicking on the thumbs up next to those answers. If any one answer lead to a solution, please indicate this by clicking on the check mark next that answer.
0 Likes 0 ·
Hi David and Maxka,thank you for helping. The issue is related to the folder permission. I don't have rights to change the folders permission.
0 Likes 0 ·
David Wimbush avatar image
David Wimbush answered
The account under which the SQL Server service is running needs to have at least read/write permissions on both folders. Does it have those permissions? And on the file itself? (The file usually inherits permissions from its folder.)
4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

yes. But here I am executing the script on server which is store on respective location and trying to access local files.
0 Likes 0 ·
It doesn't really matter where the files are. The SQL Server service account is executing this on your behalf and if it doesn't have sufficient permissions it can do it.
0 Likes 0 ·
But the file is store on path "D:\try\ t1.txt " on my local drive and not on server where DB store.
0 Likes 0 ·
If the file is not in the D: drive of the server, then xp_cmdshell will need the full path to the file on your computer, not the local path that that you would use. Create a share on your computer and make sure the SQL Service account has permission to use the share and the directories within it. Then update your move statement: 'MOVE /Y "\\full_path_to_the_file_on_your_computer\ t1.txt" "\\full_path_to_the_new_locatin_on_your_computer\"'
0 Likes 0 ·
MAXKA avatar image
MAXKA answered
@Deepika, as mentioned by David it does not matter where the files are. CORRECT. What you need to do is, : 1. Do services.msc from run 2. Locate the SQL service account running the this XP_CMDshell or the one which starts SQLserver. 3. Now once you found the name, go and make sure that account should have read write access on the folders where you are trying to copy the files. 4. Read this for more clarification : https://msdn.microsoft.com/en-us/library/ms143504.aspx 5. Also you can make sure on the folders, the Sql service account is added and given read write access.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.