Is it possible to query a folder in TSQL, from SQL Management Studio, and return a list of file names? If so, how?
3 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.
2 Comments
Faiz
I would disagree. This is like you using command line to bring information but not querying NTFS. And you cannot use any WHERE clause to say select only latest files (Modified Date filter). Good attempt though...
Thanos Markou
@Faiz The above query does exactly what OP asked though.
CLR integration is also an option, if you're not comfortable with allowing xp_cmdshell to be executed.
1 Comment
John K
+1 because I personally like the features offered by this mechanism - they have come in very useful to me for all kinds of tasks that are obtuse in TSQL. And +10 to Microsoft for creating this .NET integration feature in SQL Server.