Solved

Copying and Moving Windows Directory Files using SQL Server 2008

Posted on 2014-01-26
3
9,336 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now