Solved

Copying and Moving Windows Directory Files using SQL Server 2008

Posted on 2014-01-26
3
10,098 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
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…

733 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