Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using T-SQL to Query a File System Folder

Is it possible to query a folder in TSQL, from SQL Management Studio, and return a list of file names? If so, how?

like image 571
George Johnston Avatar asked Feb 28 '10 00:02

George Johnston


People also ask

How do I select a folder in SQL?

Select(s => new SqlString(s)); } public static void GetFiles_FillRow(object obj,out SqlString filePath) { filePath = (SqlString)obj; } }; And your SQL query. use MyDb select * From GetFiles('C:\Temp\'); Be aware though, your database needs to have CLR Assembly functionaliy enabled using the following SQL Command.

How do I find the database path to a file in SQL Server?

You have two native options for finding out where the SQL server stores its database files: either right-click on the instance name in SQL Server Management Studio (SSMS) and navigate to the 'Database Settings' tab, or use a T-SQL query.


1 Answers

You can use xp_cmdshell.

Example:

EXECUTE master.dbo.xp_cmdshell 'DIR "C:\YourDirectory\" /A-D /B'

There's a good, full example with more options here.

like image 150
AdaTheDev Avatar answered Sep 27 '22 02:09

AdaTheDev