To Get All File Details From Local File Into SQL Server

Hi Friends,

After posting on CDC for a long time, I think I will discuss an easy concept first rather than a hard one. Today's Stored Procedure is quite simple and easy to implement.

Requirement

We have a File stored locally on the computer, and we need to find all the required properties related to the file.

Solution

We just have to execute the following Stored Procedure.

  1. CREATE PROCEDURE xp_getfiledetails @filename NVARCHAR(255) = NULL --(full path)  
  2. AS DECLARE @fileobj INT,  
  3. @fsobj INT DECLARE @exists INT,  
  4. @error INT DECLARE @src VARCHAR(255),  
  5. @desc VARCHAR(255) --create FileSystem Object  
  6. EXEC @error = sp_OACreate 'Scripting.FileSystemObject',  
  7. @fsobj OUT IF @error <> 0 BEGIN EXEC sp_OAGetErrorInfo @fsobj,   
  8. @src OUT,   
  9. @desc OUT   
  10. SELECT   
  11.   error = CONVERT(  
  12.     varbinary(4),   
  13.     @error  
  14.   ),   
  15.   Source = @src,   
  16.   Description = @desc RETURN 2 END --check if specified file exists  
  17.   EXEC @error = sp_OAMethod @fsobj,   
  18.   'FileExists',   
  19.   @exists OUT,   
  20.   @filename IF @exists = 0 BEGIN RAISERROR 22004 'The system cannot find the file specified.' RETURN 2 END --Create file object that points to specified file  
  21.   EXEC @error = sp_OAMethod @fsobj,   
  22.   'GetFile',   
  23.   @fileobj OUTPUT,   
  24.   @filename IF @error <> 0 BEGIN EXEC sp_OAGetErrorInfo @fsobj RETURN 2 END --Declare variables holding properties of file  
  25.   DECLARE @Attributes TINYINT,   
  26.   @DateCreated DATETIME,   
  27.   @DateLastAccessed DATETIME,   
  28.   @DateLastModified DATETIME,   
  29.   @Drive VARCHAR(1),   
  30.   @Name NVARCHAR(255),   
  31.   @ParentFolder NVARCHAR(255),   
  32.   @Path NVARCHAR(255),   
  33.   @ShortPath NVARCHAR(255),   
  34.   @Size INT,   
  35.   @Type NVARCHAR(255) --Get properties of fileobject  
  36.   EXEC sp_OAGetProperty @fileobj,   
  37.   'Attributes',   
  38.   @Attributes OUT EXEC sp_OAGetProperty @fileobj,   
  39.   'DateCreated',   
  40.   @DateCreated OUT EXEC sp_OAGetProperty @fileobj,   
  41.   'DateLastAccessed',   
  42.   @DateLastAccessed OUT EXEC sp_OAGetProperty @fileobj,   
  43.   'DateLastModified',   
  44.   @DateLastModified OUT EXEC sp_OAGetProperty @fileobj,   
  45.   'Drive',   
  46.   @Drive OUT EXEC sp_OAGetProperty @fileobj,   
  47.   'Name',   
  48.   @Name OUT EXEC sp_OAGetProperty @fileobj,   
  49.   'ParentFolder',   
  50.   @ParentFolder OUT EXEC sp_OAGetProperty @fileobj,   
  51.   'Path',   
  52.   @Path OUT EXEC sp_OAGetProperty @fileobj,   
  53.   'ShortPath',   
  54.   @ShortPath OUT EXEC sp_OAGetProperty @fileobj,   
  55.   'Size',   
  56.   @Size OUT EXEC sp_OAGetProperty @fileobj,   
  57.   'Type',   
  58.   @Type OUT --destroy File Object  
  59.   EXEC @error = sp_OADestroy @fileobj IF @error <> 0 BEGIN EXEC sp_OAGetErrorInfo @fileobj RETURN END --destroy FileSystem Object  
  60.   EXEC @error = sp_OADestroy @fsobj IF @error <> 0 BEGIN EXEC sp_OAGetErrorInfo @fsobj RETURN 2 END --return results  
  61. SELECT   
  62.   NULL AS [Alternate Name],   
  63.   @Size AS [Size],   
  64.   CONVERT(varchar, @DateCreated, 112) AS [Creation Date],   
  65.   REPLACE(  
  66.     CONVERT(varchar, @DateCreated, 108),   
  67.     ':',   
  68.     ''  
  69.   ) AS [Creation Time],   
  70.   CONVERT(varchar, @DateLastModified, 112) AS [Last Written Date],   
  71.   REPLACE(  
  72.     CONVERT(varchar, @DateLastModified, 108),   
  73.     ':',   
  74.     ''  
  75.   ) AS [Last Written Time],   
  76.   CONVERT(varchar, @DateLastAccessed, 112) AS [Last Accessed Date],   
  77.   REPLACE(  
  78.     CONVERT(varchar, @DateLastAccessed, 108),   
  79.     ':',   
  80.     ''  
  81.   ) AS [Last Accessed Time],   
  82.   @Attributes AS [Attributes] --EOF--   

Before that, we need to make sure about the configuration of the following SQL Server Option.

  1. --WARNING! ERRORS ENCOUNTERED DURING SQL PARSING!   
  2. /**********************************************************************/  
  3. --Execute the code only once  
  4. sp_configure 'show advanced options'  
  5.     ,1;  
  6. GO  
  7.   
  8. RECONFIGURE;  
  9. GO  
  10.   
  11. sp_configure 'Ole Automation Procedures'  
  12.     ,1;  
  13. GO  
  14.   
  15. RECONFIGURE;  
  16. GO  
  17.   
  18. Test Run  
  19. FOR same Stored PROCEDURE :  
  20.     /***************************************************************************************/  
  21.     --Test RUN DECLARE @FILEPATH VARCHAR(50) set @FILEPATH='D:\new\BBP.dtsConfig' EXEC xp_getfiledetails @FILEPATH  

Hope this helps !!!!

Up Next
    Ebook Download
    View all
    Learn
    View all