Home > SQL Server Tips > Database Development > Stored procedure: Retrieve filtered file lists from a specified directory
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE DEVELOPMENT

Stored procedure: Retrieve filtered file lists from a specified directory


Brian Walker, Contributor
06.20.2005
Rating: -4.50- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


This is the second in a series of tips that present SQL code for a variety of utility stored procedures. In the last tip, A simple stored procedure to list database objects by selected type(s), we explained that these stored procedures are primarily for administrative purposes, and they are designed to be installed as system stored procedures. This tip continues the system stored procedure series with another simple routine.

The SQL code in Listing 1 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.


Click for the stored procedure: sp_ListFiles


About the author: Brian Walker is a senior database architect in an IS department that uses SQL Server 2000 and the .NET Framework. He has more than 25 years of experience in the IT industry with the last several years focused on databases and SQL Server. Walker is a software developer, database developer, database administrator and database consultant. He develops utility software as a hobby, including a large collection of SQL Server utilities.


Do you have comments on this tip? Let us know.

Rate this Tip
To rate tips, you must be a member of SearchSQLServer.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
SQL Server Stored Procedures
SQL Server Mailbag: CALs, witnesses and unwanted changes
SQL Server Mailbag: Stored procedures, triggers and SSRS reports
Top tips and tricks for SQL Server database development
Top 10 SQL Server development tips of 2008
SQL Server trigger vs. stored procedure to receive data notification
SQL Server errors, failures and other problems fixed from the trenches
SQL Server and data manipulation in T-SQL
How to use SQL Server 2008 hierarchyid data type
SQL Server stored procedures tutorial: Write, tune and get examples
Check SQL Server database and log file size with this stored procedure

SQL/Transact SQL (T-SQL)
SQL language crash course (just enough to be dangerous)
Working with IntelliSense in SQL Server 2008 Management Studio
SQL Server Mailbag: Stored procedures, triggers and SSRS reports
Working with sparse columns in SQL Server 2008
Determining the source of full transaction logs in SQL Server
New GROUP BY option provides better data control in SQL Server 2008
Using the OPENROWSET function in SQL Server
Loading data files with SQL Server's BULK INSERT statement
Importing and exporting bulk data with SQL Server's bcp utility
Testing transaction log autogrowth behavior in SQL Server
SQL/Transact SQL (T-SQL) Research

Database Development
Speed up reports in SQL Server Reporting Services with caching
Data Transformation Services vs. SSIS: The key differences
Working with IntelliSense in SQL Server 2008 Management Studio
Top tips and tricks for SQL Server database development
Managing the development lifecycle with Visual Studio Team System 2008
Processing XML files with SQL Server functions
A first look at Visual Studio Team System 2008 Database Edition
How to create a SQL inner join and outer join: Basics to get started
New datetime data types in SQL Server 2008 offer flexibility
Using DATEADD and DATEDIFF to calculate SQL Server datetime values

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
library  (SearchSQLServer.com)
trigger  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SQL Server Development - .NET, C#, T-SQL, Visual Basic
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts