Function and SP to find if file/directory exists

,

This function and stored procedure can be used to easily identify if a file, directory or parent directory exist.
It wraps the functionality of xp_fileExist but gives easy access to the directory and parent directory exist values.

To use it the function pass in the file or path and either 'IsFile', 'IsDir' or 'HasParentDir' it will return 1 if true.

--##################################################################################################################################
IF EXISTS ( SELECT  1 FROM msdb.dbo.sysobjects WHERE name = 'fn_FileExist')
  DROP FUNCTION dbo.fn_FileExist
GO
CREATE FUNCTION dbo.fn_FileExist
/*******************************************************************************

  Written By  : Simon Sabin
  Date        : 12 November 2002
  Description : Checks if a file/directory exists
              : The property value can take the values
              : IsFile = Will return 1 if the File passed in is a file
              : IsDir  = Will return 1 if the File passed in is a directory
              : HasParentDir = Will return 1 if parent directory of the file 
                               passed in exists
  History
  Date       Change
  ------------------------------------------------------------------------------
  12/11/2002 Created
*******************************************************************************/
(
  @file     text
 ,@property varchar(100)
)
RETURNS bit
AS
  BEGIN
  DECLARE @status bit

  EXEC @status = dbo.usp_FileExist @file, @property

  RETURN @status
  END
GO
IF EXISTS (SELECT 1 FROM msdb.dbo.sysobjects WHERE name = 'usp_FileExist')
  DROP PROCEDURE dbo.usp_FileExist
GO
CREATE PROCEDURE dbo.usp_FileExist 
/*******************************************************************************

  Written By  : Simon Sabin
  Date        : 12 November 2002
  Description : Checks if a file/directory exists. Used with the function fn_FileExist
              : 
  History
  Date       Change
  ------------------------------------------------------------------------------
  12/11/2002 Created
*******************************************************************************/
(
  @file     text
 ,@property varchar(100)
)
AS
  DECLARE @status bit
  CREATE TABLE #FileExists (isFile bit, isDir bit, hasParentDir bit)

  INSERT INTO #FileExists exec master.dbo.xp_fileexist @file

  IF @property = 'IsFile'
    SELECT @status = isFile
      FROM #FileExists
  IF @property = 'IsDir'
    SELECT @status = isDir
      FROM #FileExists
  IF @property = 'HasParentDir'
    SELECT @status = hasParentDir
      FROM #FileExists

  DROP TABLE #FileExists
  RETURN @status
GO

Rate

1.33 (3)

Share

Share

Rate

1.33 (3)