SQLUSA

Microsoft SQL Server 2005 Best Practices

 

How to get a list of files in a folder?

 

You can get a list of files or subfolders into a temporary or work table by using command shell "dir" command. Once the list in the table, you have to scrub it, and then you can use it in data transfer operations. Here is a script example:

 

 

create table #FileList ( Line varchar(512))

insert #FileList

exec master.dbo.xp_cmdshell 'dir f:\data\ /A-D  /B'

delete #FileList where Line is null

select * from #FileList

go

 

create table #FolderList ( Line varchar(512))

insert #FolderList

exec master.dbo.xp_cmdshell 'dir f:\data\ /A-A  /B'

delete #FolderList where Line is null

select * from #FolderList

go

 

 

The World Leader in SQL Server Training
 
SQLUSA.com Home Page