DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
SITE SEARCH SQLUSA.com HEADLINES NEWS
SQL E/BOOKS   SQL 2014 PROGRAMMING   DOWNLOADS
SCRIPTS SQL 2005 SQL 2008 ARTICLES
SQL JOBS TWITTER FORMAT VIDEOS
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) - dynamic command 
-- T-SQL list files - MSSQL insert exec - T-SQL commmand shell - xp_cmdshell
CREATE TABLE #FileList (
  Line VARCHAR(512))
DECLARE @CMD nvarchar(256)
SET @CMD = 'dir f:\data\ /A-D  /B'
INSERT #FileList
EXEC MASTER.dbo.xp_cmdshell @CMD
 
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))
DECLARE @CMD nvarchar(256)
SET @CMD = 'dir f:\data\ /A-A  /B'
INSERT #DirectoryList
EXEC MASTER.dbo.xp_cmdshell @CMD
 
DELETE #DirectoryList
WHERE  Line IS NULL
 
SELECT *
FROM   #DirectoryList
GO

Related articles:

 

How to list all files in a directory?

 

List filenames from a directory into a table from SP.

 

Import multiple Files to SQL Server using T-SQL 

 

 

Exam Prep 70-461
Exam 70-461
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE