Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


Click here to copy the following block
/*
--**************************************
--  
-- Name: sp_getdir
-- Description:Gets the contents of a di
--   rectory into a #temp table
-- By: Keith Henry
--
-- Inputs:share/directory to read
--
-- Returns:table with files, file sizes
--   and subdirectorys
--
-- Side Effects:rename from sp_ if you a
--   ren't putting this in the master base

Takes a directory or UNC path AND returns the contents AS a table.
eg:
EXEC sp_getdir '\\myMachinename\myshare'
EXEC sp_getdir 'c:\temp'

returns:
datestring, timestring, directory, filesize, nameoffile
It handles the errors WITH a useful message.
eg:
EXEC sp_getdir '\\myMachine\c$'  ---->(where I'm NOT sysadmin)

might return:
CURRENT user's login IS NOT a member OF the sysadmin role
Non sysadmin executions OF xp_cmdshell currently run as: MYDOMAIN\AUSER
You can change this WITH xp_sqlagent_proxy_account N'SET', <domain>, <username>, <password>
*/

CREATE PROC sp_getdir (@networkpath varchar(1000)) AS


  begin
      SET nocount ON
      CREATE TABLE #temp([output] varchar(8000))
      DECLARE @cmdstr varchar(1000)
      SELECT @cmdstr = 'dir ' + @networkpath + ' /A:D /A:S /A:H /A:R /A:A /-C /N /4 '
      
      INSERT #temp
      EXEC master.dbo.xp_cmdshell @cmdstr
      
      SELECT left(t.[output],10) AS datestring, substring(t.[output],13,5) as timestring,
          CASE substring(t.[output],26,3) WHEN 'DIR' THEN 1 ELSE 0 END as directory ,
          CASE substring(t.[output],26,3) WHEN 'DIR' THEN NULL ELSE cast(ltrim(substring(t.[output],20,19)) as int) END as filesize,
          substring(t.[output],40,1000) AS nameoffile
      FROM #temp AS t WHERE t.[output] LIKE '[0-9][0-9]%'
      IF @@error <> 0 or NOT @@rowcount > 0 GOTO doh
      
      GOTO done
      doh:
      
      IF exists(select * FROM #temp WHERE rtrim(ltrim([output])) = 'The network path was NOT found.')
          PRINT @networkpath + ' was NOT found.'
      ELSE
      BEGIN
          IF exists(select * FROM #temp WHERE rtrim(ltrim([output])) = 'Logon failure: unknown USER name or bad password.')
          BEGIN
              PRINT 'Login failure TO ' + @networkpath
              IF is_srvrolemember ('sysadmin') = 1
                  PRINT 'Current user''s login IS a member OF the sysadmin role' + char(10) +
                      'The account MSSQL runs under does NOT have access TO ' + @networkpath
              ELSE
              BEGIN
                  PRINT 'Current user''s login IS NOT a member OF the sysadmin role'
                  DECLARE @Domain sysname, @Username sysname
                  CREATE TABLE #temp2(Domain sysname, Username sysname)
                  INSERT #temp2
                  EXEC master.dbo.xp_sqlagent_proxy_account N'GET'
                  
                  SELECT @Domain = t.Domain, @Username = t.Username FROM #temp2 t
                  PRINT 'Non sysadmin executions OF xp_cmdshell currently run as: ' + isnull(@Domain + '\' + @Username, 'No user set')
                  DROP TABLE #temp2
                  PRINT 'You can change this WITH xp_sqlagent_proxy_account N''SET'', <domain>, <username>, <password>'
              END
          END
          ELSE
              SELECT * FROM #temp
      END
      
      done:
      DROP TABLE #temp
      SET nocount OFF
end
go
EXEC sp_getdir '\\server\share'


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.