SQLUSA
Free Trial Save on Combos
SQL Server 2008 Best Practices
SQL Server 2005 Best Practices
SQL Server 2000 Best Practices
How to get a list of files in a folder?

Execute the following Microsoft SQL Server T-SQL scripts in Management Studio Query Editor to obtain a list of files and directories (subfolders) in the specified directory.

-- Get file list in specified folder (directory)

-- T-SQL list files - MSSQL insert exec - T-SQL commmand shell - xp_cmdshell

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

 

-- Get subfolder (subdirectory) list in specified folder

-- T-SQL list directories - create temporary table

CREATE TABLE #DirectoryList (

  Line VARCHAR(512))

 

INSERT #DirectoryList

EXEC MASTER.dbo.xp_cmdshell

  'dir f:\data\ /A-A  /B'

 

DELETE #DirectoryList

WHERE  Line IS NULL

 

SELECT *

FROM   #DirectoryList

 

GO

------------

 

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