Advertisement
Guest User

Untitled

a guest
May 27th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.57 KB | None | 0 0
  1. IF EXISTS ( SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[usp_DirectoryExists]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
  2.     DROP PROCEDURE [dbo].[usp_DirectoryExists]
  3. GO
  4.  
  5. SET QUOTED_IDENTIFIER ON
  6. GO
  7. SET ANSI_NULLS ON
  8. GO
  9.  
  10. /*
  11.     DECLARE @pathName VARCHAR(255);
  12.     SET @pathName =
  13.         'C:\Windows';
  14.        
  15.     DECLARE @argStatusInd BIT;
  16.     EXEC dbo.usp_DirectoryExists
  17.         @argPathName = @pathName,
  18.         @argStatusId = @argStatusInd OUTPUT
  19.     SELECT @argStatusInd;
  20. */
  21.  
  22. /*******************************************************************************
  23.  
  24.     Procedure name: usp_DirectoryExists
  25.     Date Modified:  05/24/2010
  26.     Request #:     
  27.     Notes:          Simple utility SP that checks if a directory exists
  28.                     in the FS. Returns 1 (true) if it does, returns 0 (false)
  29.                     if not.
  30.  
  31. ********************************************************************************/
  32.  
  33. CREATE PROCEDURE [dbo].[usp_DirectoryExists]
  34.     @argPathName    VARCHAR(255),
  35.     @argStatusId    BIT output
  36. AS
  37.  
  38. SET NOCOUNT ON;
  39.  
  40. DECLARE @execCmd VARCHAR(2000);
  41. DECLARE @result INT;
  42.  
  43. /*  Get a directory listing for a given path. It'll return an error
  44.     if the directory doesn't exist, so we can use that to determine
  45.     how to handle our returned value. */
  46. SET @argStatusId = 1;
  47. SET @execCmd = 'dir ' + @argPathName;
  48.  
  49. EXEC @result = master.dbo.xp_cmdshell @execCmd, no_output;
  50.  
  51. IF @result <> 0
  52. BEGIN
  53.     --PRINT 'Directory doesn''t exist.'
  54.     SET @argStatusId = 0
  55.     RETURN
  56. END
  57.  
  58. SET NOCOUNT OFF;
  59.  
  60. GO
  61. SET QUOTED_IDENTIFIER OFF
  62. GO
  63. SET ANSI_NULLS ON
  64. GO
  65.  
  66. GRANT  EXECUTE  ON [dbo].[usp_DirectoryExists] TO [public]
  67. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement