Using T-SQL to query a file system folder

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

+7
sql-server tsql
source share
2 answers

You can use xp_cmdshell.

Example:

EXECUTE master.dbo.xp_cmdshell 'DIR "C:\YourDirectory\" /AD /B' 

There is a good, complete example with additional options here .

+13
source share

Integration with the CLR is also possible if you are uncomfortable with allowing xp_cmdshell to run.

See MSDN .

+6
source share

All Articles