Solved

Copying and Moving Windows Directory Files using SQL Server 2008

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

746 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

11 Experts available now in Live!

Get 1:1 Help Now