Here is one way. You create a temporary table and fill it with the contents of a directory, then check the table for your file name, and finally delete your temporary table.
Just replace '%test.txt%' with your filename (keep the percentage signs) and replace 'dir C:\' with 'dir [yourpath]'
Code:
SET NOCOUNT ON
CREATE TABLE #tbl (tblcontents VarChar(2000))
INSERT INTO #tbl exec master.dbo.xp_cmdshell 'dir C:\'
IF EXISTS (SELECT * FROM #tbl WHERE tblcontents LIKE '%test.txt%')
SELECT 'exists'
ELSE
SELECT 'does not exist'
DROP TABLE #tbl
Denny is right, I've just found this myself. In the context of your query, you can use this:
Code:
declare @result int
exec xp_fileexist ‘filename’, @result output
IF (@result = 1)
bulk insert tablename from 'filename'
else
-- report that file does not exist
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.