How do I check if a directory exists using SQL Server?

RJ. picture RJ. · Dec 7, 2012 · Viewed 28.5k times · Source

I know this command will create a directory:

EXEC master.sys.xp_create_subdir 'C:\testing\'

But how do I check whether 'C:\testing\' exists?

IF EXISTS(...

Answer

Shiraz Bhaiji picture Shiraz Bhaiji · Dec 7, 2012
 CREATE TABLE ResultSet (Directory varchar(200))

 INSERT INTO ResultSet
 EXEC master.dbo.xp_subdirs 'c:\'

 Select * FROM ResultSet where Directory = 'testing'

Will return a list of sub directories, you can then check the contents of the list.