如何使用sql脚本查找文件夹大小? [英] how to find folder size using sql scripts?

查看:114
本文介绍了如何使用sql脚本查找文件夹大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用sql脚本查找文件夹大小?

[OP的答案移至问题]
您好,这里是要查找的代码.

但是要抓住的是这段代码,我可以找到该文件夹​​内文件的大小.不是所有文件和子文件夹大小的总和.如果我有子文件夹而不是单个文件,那么它将显示0字节.

我可以知道为什么以及如何计算文件夹的大小,包括子文件夹的大小.

how to find folder size using sql scripts?

[OP''s answer moved to question]
Hi here is the code to find.

but the catch is with this code i can find size of files inside the folder. Not the sum of all files and subfolder size.and if i have sublfoders not an indivdual files then it shows 0 bytes.

May i known why and how to calculate size of folder including subfolder size.

use tempdb
set nocount on
 
declare @line varchar(255)
declare @path varchar(255)
declare @command varchar(255)
 
if object_id(''#output'') is not null and objectproperty(object_id(''#output''),
''IsTable'') = 1
drop table #output
 
create table #output (line varchar(255))
 
set @path = ''C:\Program Files''
set @command = ''dir "'' + @path +''"''
 
insert into #output
exec master.dbo.xp_cmdshell @command
 
select @line = ltrim(replace(substring(line, charindex('')'', line)+1,len(line)), '','', ''''))
from #output where line like ''%File(s)%bytes''
 
print @line + '' (This is a string and the value ignores the size of any sub-folders)''

推荐答案

一个简单的Google搜索,使用问题文本的剪切粘贴功能返回了约260万个结果.
我在前10个结果中找到了您问题的答案.

^ ]

来自 http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=156137 [ ^ ]:
您可以使用xp_cmdshell调用带有适当标志的dir命令,或者使用Powershell脚本来调用dir命令,但是仅此而已.没有本机SQL命令可以提供该信息."

[edit]
根据您的其他信息,我并不完全熟悉此操作,但是您可能需要创建一个过程,该过程将创建文件大小总和的运行总计,然后遍历每个子文件夹.如果目录结构达到任何实际大小,这将是一个非常低效且耗时的过程,因此希望有人也可以为您提供更好的信息.
[/edit]
A simple google search using a cut-and-paste of your question text returned about 2.6 million results.
I found an answer to your question in the first 10 results.

how to find folder size using sql scripts[^]

from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=156137[^]:
"You can use xp_cmdshell to call the dir command with the appropriate flags, or maybe a Powershell script, but that''s about it. There are no native SQL commands to provide that information."

[edit]
As per your additional information, I am not entirely familiar with doing this, but you may need to create a process that will create a running tally of the sum of file sizes and then iterate through each sub folder. This would be a very inefficient and time consuming process if the directory structure gets to any substantial size, so hopefully someone can provide you better info as well.
[/edit]


这篇关于如何使用sql脚本查找文件夹大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆