Solved

Copying and Moving Windows Directory Files using SQL Server 2008

Posted on 2014-01-26
3
10,519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

717 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