Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to calculate size of a folder located on different machine(Remote) in sql server 2008?

$
0
0

DECLARE @TenantId UNIQUEIDENTIFIER ='A79ED820-2E5E-4A9D-B930-B7597DC97081'
DECLARE @BusinessName VARCHAR(200)

declare @line  varchar(255) =''
declare @path varchar(255)=''
declare @command varchar(255)=''
DECLARE @folderSizeGB decimal (16,8) = 0

DECLARE @AttachmentDiscSpaceUsed decimal (16,8) = 0

-- Code to calculate folder size in GB BEGINs

            IF OBJECT_ID('tempdb..#temp') IS NOT NULL
            drop table #temp

            create table #temp (line varchar(255))

            --set @path = '\\ewp-dev18\\c$\\Attachments\\' +  CONVERT(varchar(50),@tenantId)
            set @path = '-S ewp-dev18 -E -i ewp-dev18\\c$\\Attachments\\' +  CONVERT(varchar(50),@tenantId)
            set @command = 'dir "' + @path +'"'                                

            insert into #temp
            exec master.dbo.xp_cmdshell @command

            select
            @line=
            ltrim(replace(substring(line, charindex(')', line)+1,
            len(line)), ',', ''))
            from #temp where line like '%File(s)%bytes'

            SET @line=
            (Case WHEN @line IS NULL or @line ='' THEN '0' ELSE @line END)

            set @folderSizeGB  = Cast(Replace(@line,'bytes', '') as decimal)/1073741824
            if (@folderSizeGB is null )
            set @folderSizeGB =0

            SET @AttachmentDiscSpaceUsed = @folderSizeGB

            SET @path=''
            SET @command=''
            SET @line=0
            -- Drop #temp table
            IF OBJECT_ID('tempdb..#temp') IS NOT NULL
            drop table #temp

            -- Code to calculate folder size in GB ENDs

            

Viewing all articles
Browse latest Browse all 23857

Trending Articles