PABluesMan
Starting Member
26 Posts |
Posted - 2008-03-25 : 16:46:50
|
Here's a table function that returns a table variable containing various file properties (creation date, size, etc.). The one limitation to remember is that it won't work with files on mapped drives; you have to use the full \\<Server>\<Share>\<Folder1..Folder n>\<Filename> syntax. Still, it's a pretty useful thing to have around. :)/****************************************************************************/CREATE FUNCTION fnc_GetFileProps(@FileName VARCHAR (1024))RETURNS @Results TABLE ( ErrorCode TINYINT DEFAULT (0), PropName VARCHAR (255), PropValue SQL_VARIANT )ASBEGINDECLARE @OLEResult INTDECLARE @FS INTDECLARE @FileID INTDECLARE @Message VARCHAR (8000)DECLARE @ErrorSource VARCHAR (255)DECLARE @ErrorDesc VARCHAR (255)DECLARE @INT INTDECLARE @VARCHAR VARCHAR (1024)DECLARE @DATETIME DATETIMEDECLARE @BIGINT BIGINT-- Create an instance of the file system objectEXECUTE @OLEResult = sp_OACreate 'Scripting.FileSystemObject', @FS OUTIF @OLEResult <> 0 BEGIN EXEC sp_OAGetErrorInfo @FS, @ErrorSource OUT, @ErrorDesc OUT INSERT @Results (ErrorCode, PropName, PropValue) VALUES (1, @ErrorSource, @ErrorDesc) RETURN ENDEXEC @OLEResult = sp_OAMethod @FS, 'GetFile', @FileID OUT, @FilenameIF @OLEResult <> 0 BEGIN EXEC sp_OAGetErrorInfo @FS, @ErrorSource OUT, @ErrorDesc OUT INSERT @Results (ErrorCode, PropName, PropValue) VALUES (1, @ErrorSource, @ErrorDesc) RETURN ENDEXEC @OLEResult = sp_OAGetProperty @FileID, 'Attributes', @INT OUTIF @OLEResult <> 0 BEGIN INSERT @Results (ErrorCode, PropName, PropValue) VALUES (1, 'Attributes', '<ERROR RETRIEVING PROPERTY>') ENDELSE INSERT @Results (PropName, PropValue) VALUES ('Attributes', @INT)EXEC @OLEResult = sp_OAGetProperty @FileID, 'DateCreated', @DATETIME OUTIF @OLEResult <> 0 BEGIN INSERT @Results (ErrorCode, PropName, PropValue) VALUES (1, 'DateCreated', '<ERROR RETRIEVING PROPERTY>') ENDELSE INSERT @Results (PropName, PropValue) VALUES ('DateCreated', @DATETIME)EXEC @OLEResult = sp_OAGetProperty @FileID, 'DateLastAccessed', @DATETIME OUTIF @OLEResult <> 0 BEGIN INSERT @Results (ErrorCode, PropName, PropValue) VALUES (1, 'DateLastAccessed', '<ERROR RETRIEVING PROPERTY>') ENDELSE INSERT @Results (PropName, PropValue) VALUES ('DateLastAccessed', @DATETIME)EXEC @OLEResult = sp_OAGetProperty @FileID, 'DateLastModified', @DATETIME OUTIF @OLEResult <> 0 BEGIN INSERT @Results (ErrorCode, PropName, PropValue) VALUES (1, 'DateLastModified', '<ERROR RETRIEVING PROPERTY>') ENDELSE INSERT @Results (PropName, PropValue) VALUES ('DateLastModified', @DATETIME)EXEC @OLEResult = sp_OAGetProperty @FileID, 'Name', @VARCHAR OUTIF @OLEResult <> 0 BEGIN INSERT @Results (ErrorCode, PropName, PropValue) VALUES (1, 'Name', '<ERROR RETRIEVING PROPERTY>') ENDELSE INSERT @Results (PropName, PropValue) VALUES ('Name', @VARCHAR)EXEC @OLEResult = sp_OAGetProperty @FileID, 'Path', @VARCHAR OUTIF @OLEResult <> 0 BEGIN INSERT @Results (ErrorCode, PropName, PropValue) VALUES (1, 'Path', '<ERROR RETRIEVING PROPERTY>') ENDELSE INSERT @Results (PropName, PropValue) VALUES ('Path', @VARCHAR)EXEC @OLEResult = sp_OAGetProperty @FileID, 'ShortPath', @VARCHAR OUTIF @OLEResult <> 0 BEGIN INSERT @Results (ErrorCode, PropName, PropValue) VALUES (1, 'ShortPath', '<ERROR RETRIEVING PROPERTY>') ENDELSE INSERT @Results (PropName, PropValue) VALUES ('ShortPath', @VARCHAR)EXEC @OLEResult = sp_OAGetProperty @FileID, 'Size', @BIGINT OUTIF @OLEResult <> 0 BEGIN INSERT @Results (ErrorCode, PropName, PropValue) VALUES (1, 'Size', '<ERROR RETRIEVING PROPERTY>') ENDELSE INSERT @Results (PropName, PropValue) VALUES ('Size', @BIGINT)EXEC @OLEResult = sp_OAGetProperty @FileID, 'Type', @VARCHAR OUTIF @OLEResult <> 0 BEGIN INSERT @Results (ErrorCode, PropName, PropValue) VALUES (1, 'Type', '<ERROR RETRIEVING PROPERTY>') ENDELSE INSERT @Results (PropName, PropValue) VALUES ('Type', @VARCHAR)EXECUTE @OLEResult = sp_OADestroy @FileIDEXECUTE @OLEResult = sp_OADestroy @FSRETURNEND/****************************************************************************/I geek, therefore I am |
|