create function dbo.ufn_getLastModifDate(@FileName varchar(250)) returns varchar(100) as begin declare @strDate varchar(100), @objFSO int, @objFile int exec sp_oacreate 'Scripting.FileSystemObject', @objFso out exec sp_oamethod @objFso, 'GetFile', @objFile out, @FileName exec sp_oagetproperty @objFile, 'DateLastModified', @strDate out exec sp_oadestroy @objFile exec sp_oadestroy @objFso return @strDate end select dbo.ufn_getLastModifDate('c:\temp\somefile.txt')>>Is there a way to retreive the modified date of a file (that I know the name and the path) from a stored procedure ?
>IF EXISTS (SELECT * FROM sysobjects > WHERE name = N'usp_FileDate') > DROP PROCEDURE usp_FileDate >GO >---------------------------------------------------- >CREATE PROCEDURE usp_FileDate (@FileName varchar(64), @FileDate datetime OUTPUT) >AS >BEGIN >DECLARE @RetCode int, @FileSystem int, @FileHandle int >DECLARE @YesNo int, @File int >DECLARE @hr int, @source varchar(255), @description varchar(255) > >EXECUTE @RetCode = sp_OACreate 'Scripting.FileSystemObject', @FileSystem OUTPUT >IF @@ERROR > 0 GOTO ErrorHandler >-- Check if file exists >EXECUTE @RetCode = sp_OAMethod @FileSystem, 'FileExists', @YesNo OUTPUT, @FileName >IF @@ERROR > 0 GOTO ErrorHandler >IF @RetCode > 0 BEGIN > EXEC @hr = sp_OAGetErrorInfo @FileSystem, @source OUT, @description OUT > GOTO DisplayError >END > >IF @YesNo = 0 > -- File doesn't exist > RETURN -1 >-- Get file object >EXECUTE @RetCode = sp_OAMethod @FileSystem, 'GetFile', @File OUTPUT, @FileName >IF @@ERROR > 0 GOTO ErrorHandler >IF @RetCode > 0 BEGIN > EXEC @hr = sp_OAGetErrorInfo @FileSystem, @source OUT, @description OUT > GOTO DisplayError >END >-- Read 'DateLastModified' property of the file object >EXECUTE @RetCode = sp_OAGetProperty @File, 'DateLastModified', @Filedate OUTPUT >IF @@ERROR > 0 GOTO ErrorHandler >IF @RetCode > 0 BEGIN > EXEC @hr = sp_OAGetErrorInfo @File, @source OUT, @description OUT > GOTO DisplayError >END > >RETURN 0 > >ErrorHandler: > RETURN -99 > >DisplayError: > PRINT 'OLE Automation Error Information' > IF @hr = 0 > BEGIN > PRINT ' Source: ' + @source > PRINT ' Description: ' + @description > END >RETURN -99 > >END >>
>DECLARE @RetCode int, @dt datetime >EXEC @RetCode = usp_FileDate 'C:\boot.ini', @dt OUTPUT >SELECT @dt, @RetCode >