22 Ocak 2008 Salı

SQL Server - T-Sql ile Dosya boyutunu bulma

CREATE FUNCTION udf_GetFileSize (@filename varchar(1000))
returns bigint
as
BEGIN
DECLARE @FS int
DECLARE @OLEResult int
DECLARE @FileID int
DECLARE @Size bigint
DECLARE @Flag bigint

Set @size =0
Set @Flag =0

EXECUTE @OLEResult = sp_OACreate 'Scripting.FileSystemObject', @FS OUT
EXECUTE @OLEResult = sp_OAMethod @FS, 'GetFile', @FileID OUT,@filename

IF @OLEResult <> 0
begin
set @Flag =-1
end
else
begin
EXECUTE @OLEResult = sp_OAGetProperty @FileId,'Size', @Size OUT
IF @OLEResult <> 0
begin
set @Flag =-2
end
end

EXECUTE @OLEResult = sp_OADestroy @FileID
EXECUTE @OLEResult = sp_OADestroy @FS

if @flag <> -1 and @flag <> -2
begin
set @flag = @size
end
return @flag
END

'Kullanimi
select dbo.udf_GetFileSize ('c:\temp\test.txt')

Hiç yorum yok: