Here is code you can use
1) To create and write txt file
2) To check whether file exists
DECLARE @Text VARCHAR(MAX)
,@filelocation VARCHAR(MAX)
,@cmd VARCHAR(100)
SET @Text = 'abcdefghijklmnopqrstuvwxyz'
SET @filelocation = 'D:\out_vid.txt'
SET @cmd ='echo ' + @Text + ' > E:\out_vid.txt'
EXECUTE Master.dbo.xp_CmdShell @cmd
DECLARE @isExists INT
exec master.dbo.xp_fileexist 'E:\out_vid.txt',
@isExists OUTPUT
SELECT case @isExists
when 1 then 'Yes'
else 'No'
end as isExists