Important alert: (current site time 7/15/2013 8:02:13 PM EDT)
 

article

List Directories and Files using T-SQL

Email
Submitted on: 6/22/2005 5:42:26 AM
By: TheIndoctrinator !!  
Level: Intermediate
User Rating: By 2 Users
Compatibility: SQL Server 2000, SQL Server 7.0
Views: 48520
author picture
(About the author)
 
     Hi, This is my 6th submission, "An Intro to List File and/or Folders of any machine using T-SQL Producedurs." This code is not mine but of Mr. Brian Walker, senior database architect in an IS department that uses SQL Server 2000 and the .NET Framework. I am just posting it because i think that it will be of gr8 help for others... so enjoy!!.... Please feel free to send any comments/feedback...

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

Hi All,

My Friend mail me this piece of code and I found it very useful, so I am here to share this gud one you all. This code is written by Mr. Brian Walker, senior database architect in an IS department that uses SQL Server 2000 and the .NET Framework. Hope you all like it..

This SQL code creates a system stored procedure named sp_ListFiles. The routine returns a filtered list of files found in a specified directory. The sp_ListFiles stored procedure uses the xp_cmdshell extended stored procedure that comes with SQL Server.

There are security implications with using that tool. Normally, only a systems administrator can execute xp_cmdshell, but others can be granted permission. The SQL Agent Proxy Account is another way to control access to the potentially dangerous tool. See BOL for details. The SQL Server service account (or the SQL Agent proxy account) must be able to read the files in the specified directory. If the specified directory is on a network share, the account may need to be a domain account.

The sp_ListFiles stored procedure accepts five parameters. Only the first one is required.

The first parameter is a path to a directory. The path must be accessible to SQL Server (the service account or the proxy account).

The second parameter is a table name in which to insert the file/folder names. It can be a normal user table or a temporary table. If no table name is provided, the list is returned as a result set.

The third parameter is a filter for including certain names. Each name is compared to the filter using a LIKE operator, so wildcards are acceptable. For example, the value "%.doc" would include all Word documents.

The fourth parameter is a filter for excluding certain names. Each name is compared to the filter using a NOT LIKE operator, so wildcards are acceptable.

The fifth parameter determines whether files or folders are listed. A value of zero (0) returns files and a value of one (1) returns folders.

The following example lists the folders within the Program Files folder on the SQL Server box:

EXECUTE sp_ListFiles 'C:\Program Files\',NULL,NULL,NULL,1
 

The list of files returned by sp_ListFiles may consist of files that need to be imported into the database. The BULK INSERT statement works nicely for this purpose, but it does not accept a variable for the parameter that specifies the file to import. Because of this limitation it's often convenient to execute the BULK INSERT statement using dynamic SQL so the file to import can be determined on the fly.

The SQL code in Listing 2 demonstrates this by returning the contents of a README file (assuming a typical installation of SQL Server). The lines of the file are not necessarily returned in order in this example, but if an order not supported by the data itself is required it can be accomplished with an identity column in the temporary table and a format file.

I hope you find this system stored procedure to be useful.

 

The Code
USE master
GO
CREATE PROCEDURE dbo.sp_ListFiles
    @PCWrite varchar(2000),
    @DBTable varchar(100)= NULL,
    @PCIntra varchar(100)= NULL,
    @PCExtra varchar(100)= NULL,
    @DBUltra bit = 0
AS
 
SET NOCOUNT ON
 
DECLARE @Return int
DECLARE @Retain int
DECLARE @Status int
 
SET @Status = 0
 
DECLARE @Task varchar(2000)
 
DECLARE @Work varchar(2000)
 
DECLARE @Wish varchar(2000)
 
SET @Work = 'DIR ' + '"' + @PCWrite + '"'
 
CREATE TABLE #DBAZ (Name varchar(400), Work int IDENTITY(1,1))
 
INSERT #DBAZ EXECUTE @Return = master.dbo.xp_cmdshell @Work
 
SET @Retain = @@ERROR
 
IF @Status = 0 SET @Status = @Retain
IF @Status = 0 SET @Status = @Return
 
IF (SELECT COUNT(*) FROM #DBAZ) < 4
 
    BEGIN
 
    SELECT @Wish = Name FROM #DBAZ WHERE Work = 1
 
    IF @Wish IS NULL
 
        BEGIN
 
        RAISERROR ('General error [%d]',16,1,@Status)
 
        END
 
    ELSE
 
        BEGIN
 
        RAISERROR (@Wish,16,1)
 
        END
 
    END
 
ELSE
 
    BEGIN
 
    DELETE #DBAZ WHERE ISDATE(SUBSTRING(Name,1,10)) = 0 OR SUBSTRING
(Name,40,1) = '.' OR Name LIKE '%.lnk'
 
    IF @DBTable IS NULL
 
        BEGIN
 
          SELECT SUBSTRING(Name,40,100) AS Files
            FROM #DBAZ
           WHERE 0 = 0
             AND (@DBUltra  = 0    OR Name     LIKE '%<DIR>%')
             AND (@DBUltra != 0    OR Name NOT LIKE '%<DIR>%')
             AND (@PCIntra IS NULL OR SUBSTRING(Name,40,100)     LIKE  
@PCIntra)
             AND (@PCExtra IS NULL OR SUBSTRING(Name,40,100) NOT LIKE  
@PCExtra)
        ORDER BY 1
 
        END
 
    ELSE
 
        BEGIN
 
        SET @Task = ' INSERT ' + REPLACE(@DBTable,CHAR(32),CHAR(95))
                  + ' SELECT SUBSTRING(Name,40,100) AS Files'
                  + '   FROM #DBAZ'
                  + '  WHERE 0 = 0'
                  + CASE WHEN @DBUltra  = 0    THEN '' ELSE ' AND Name     
LIKE ' + CHAR(39) + '%<DIR>%' + CHAR(39) END
                  + CASE WHEN @DBUltra != 0    THEN '' ELSE ' AND Name NOT 
LIKE ' + CHAR(39) + '%<DIR>%' + CHAR(39) END
                  + CASE WHEN @PCIntra IS NULL THEN '' ELSE ' AND SUBSTRING
(Name,40,100)     LIKE ' + CHAR(39) + @PCIntra + CHAR(39) END
                  + CASE WHEN @PCExtra IS NULL THEN '' ELSE ' AND SUBSTRING
(Name,40,100) NOT LIKE ' + CHAR(39) + @PCExtra + CHAR(39) END
                  + ' ORDER BY 1'
 
        IF @Status = 0 EXECUTE (@Task) SET @Return = @@ERROR
 
        IF @Status = 0 SET @Status = @Return
 
        END
 
    END
 
DROP TABLE #DBAZ
 
SET NOCOUNT OFF
 
RETURN (@Status)
 
GO
 

Usage Example:

 

CREATE TABLE #Files (MyFile varchar(200))
 
CREATE TABLE #Lines (MyLine varchar(8000))
 
DECLARE @MyFile varchar(200), @SQL varchar(2000), @Path varchar(400)
 
SET @Path = 'C:\Program Files\Microsoft SQL Server\MSSQL\'
 
EXECUTE sp_ListFiles @Path,'#Files','%.txt',NULL,0
 
SELECT @MyFile = MyFile FROM #Files WHERE MyFile LIKE 'README%'
 
SET @SQL = 'BULK INSERT #Lines FROM ' + CHAR(39) + @Path + @MyFile + CHAR(39)
 
EXECUTE (@SQL)
 
SELECT * FROM #Lines
 
DROP TABLE #Files
 
DROP TABLE #Lines


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
6/23/2005 4:46:11 AMThe VB Guy

This is working very fine but am wondering if i could get Folder wise files report.. say just like treeview ctrl..... could you help me out...
Anyways 5 globs from me....
(If this comment was disrespectful, please report it.)

 
12/7/2009 8:14:17 AMbg_med_kacem

Thanks for this post, this works good.
Just a little problem, in my case, I replaced "SUBSTRING(Name,40,100)" by "SUBSTRING(Name,37,100)" since I had three characters missing in the name of the file.
Can you tell me the reason?
and give a general solutions, if possible.

Thanks again.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.