Solved

Copying and Moving Windows Directory Files using SQL Server 2008

Posted on 2014-01-26
3
9,834 Views
1 Endorsement
Last Modified: 2014-01-26
Ok,
I have a folder with about 500,000 files (pdf, tiff,etc...,).
I ran a script in SQL Server 2008 that listed all of the files names from the folder in a SQL Server database using XP_CMDSHELL.

What I want to do is, copy and then move the files to another location using the filenames that I have in another table on my server. Out of the 500,000 files only 120,000 will come over, but the file name of those files are in my database table.

Would like a solution using XP_CMDSHELL. I have been able to figure out how to move files from folder to folder, but not how to move files to another folder based on results from a sql server db.
1
Comment
Question by:imanym
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39810673
try this out

--let us assume the below is your table holding the file names

DECLARE @T TABLE
(
  FileName VARCHAR(1000)
)

INSERT INTO @T VALUES ('abcd.txt')

DECLARE @SQL NVARCHAR(MAX)
DECLARE @SQ NVARCHAR(4)
SET @SQ = ''''

SELECT 'EXEC XP_CMDSHELL ' + @SQ + 'MOVE <SourceDirectory>' + FileName + ' <destinationDirectory>' + @SQ 
FROM @T

Open in new window


Now replace the <SourceDirectory> with the directory where the files are residing
and replace the <destinationDirectory> with the directory where the files needs to be moved

And once you execute the above select statement you will get a list of rows in your SSMS.
Copy the result and paste it back in SSMS and execute it, that will do exactly what you need to do.
0
 

Author Comment

by:imanym
ID: 39810833
LOL...that is very clever, not what I was expecting, but very clever. I will use it and move on. Thanks. You deserve all of the points for this one.
0
 

Author Closing Comment

by:imanym
ID: 39810862
The solution was very simple and comprehensive. A quick fix to a big problem thanks...
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

791 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