SQL – How to list the files in a directory

Here is a simple script that reads the file names from a specified directory.
The results are held in a table that can be referenced, and then dropped when the script has completed.

declare @DIR as varchar(100)
set @DIR = ‘dir c:\’ — ENTER DIRECTORY PATH HERE 

use ORDERS
create table files_TMP (filenames varchar(max)) 

insert into files_TMP
exec master..xp_cmdshell @DIR 

select * from files_TMP
 
drop
table files_TMP

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s