SQL Server 如何写存储过程以获取特定目录下各个文件磁盘大小

0
要求能计算目录文件夹的大小
已邀请:
0

天善_小龙 2016-06-22 回答

推荐看下文章:
https://msdn.microsoft.com/zh-cn/library/ms190790.aspx
 
列出指定路径上的文件名和大小,看下面的存储过程
 
执行顺序:
1. 先执行以下代码:
sp_configure 'show advanced options',1
reconfigure
go
sp_configure 'xp_cmdshell',1
reconfigure
go
 
2. 再执行:
exec [dbo].[GetListOfFileWithSize] '制定路径'
 
CREATE PROCEDURE   [dbo].[GetListOfFileWithSize] 
(
@Dir VARCHAR(1000)
)
AS
---------------------------------------------------------------------------------------------
-- Variable decleration
---------------------------------------------------------------------------------------------
declare @curdir nvarchar(400)
declare @line varchar(400)
declare @Command varchar(400)
declare @counter int

DECLARE @1MB DECIMAL
SET @1MB = 1024 * 1024

DECLARE @1KB DECIMAL
SET @1KB = 1024

---------------------------------------------------------------------------------------------
-- Temp tables creation
---------------------------------------------------------------------------------------------
CREATE TABLE #dirs (DIRID int identity(1,1), directory varchar(400))
CREATE TABLE #tempoutput (line varchar(400))
CREATE TABLE output (Directory varchar(400), FilePath VARCHAR(400), SizeInMB DECIMAL(13,2), SizeInKB DECIMAL(13,2))

CREATE TABLE #tempFilePaths (Files VARCHAR(500))
CREATE TABLE #tempFileInformation (FilePath VARCHAR(500), FileSize VARCHAR(100))

---------------------------------------------------------------------------------------------
-- Call xp_cmdshell
---------------------------------------------------------------------------------------------

SET @Command = 'dir "'+ @Dir +'" /S/O/B/A:D'
INSERT INTO #dirs exec xp_cmdshell @Command
INSERT INTO #dirs SELECT @Dir
SET @counter = (select count(*) from #dirs)

---------------------------------------------------------------------------------------------
-- Process the return data
---------------------------------------------------------------------------------------------

WHILE @Counter <> 0
BEGIN
DECLARE @filesize INT
SET @curdir = (SELECT directory FROM #dirs WHERE DIRID = @counter)
SET @Command = 'dir "' + @curdir +'"'
------------------------------------------------------------------------------------------
-- Clear the table
DELETE FROM #tempFilePaths


INSERT INTO #tempFilePaths
EXEC MASTER..XP_CMDSHELL @Command

--delete all directories
DELETE #tempFilePaths WHERE Files LIKE '%<dir>%'

--delete all informational messages
DELETE #tempFilePaths WHERE Files LIKE ' %'

--delete the null values
DELETE #tempFilePaths WHERE Files IS NULL

--get rid of dateinfo
UPDATE #tempFilePaths SET files =RIGHT(files,(LEN(files)-20))

--get rid of leading spaces
UPDATE #tempFilePaths SET files =LTRIM(files)

--split data into size and filename
----------------------------------------------------------
-- Clear the table
DELETE FROM #tempFileInformation;

-- Store the FileName & Size
INSERT INTO #tempFileInformation
SELECT
RIGHT(files,LEN(files) -PATINDEX('% %',files)) AS FilePath,
LEFT(files,PATINDEX('% %',files)) AS FileSize
FROM #tempFilePaths

--------------------------------
-- Remove the commas
UPDATE #tempFileInformation
SET FileSize = REPLACE(FileSize, ',','')




--------------------------------------------------------------
-- Store the results in the output table
--------------------------------------------------------------

INSERT INTO output--(FilePath, SizeInMB, SizeInKB)
SELECT
@curdir,
FilePath,
CAST(CAST(FileSize AS DECIMAL(13,2))/ @1MB AS DECIMAL(13,2)),
CAST(CAST(FileSize AS DECIMAL(13,2))/ @1KB AS DECIMAL(13,2))
FROM #tempFileInformation

--------------------------------------------------------------------------------------------


Set @counter = @counter -1
END


DELETE FROM OUTPUT WHERE Directory is null
----------------------------------------------
-- DROP temp tables
----------------------------------------------
DROP TABLE #Tempoutput
DROP TABLE #dirs
DROP TABLE #tempFilePaths
DROP TABLE #tempFileInformation
--DROP TABLE #tempfinal

SELECT * FROM OutPut
DROP TABLE output
1

- 取是能力,舍是境界 2016-06-22 回答

SQL 数据库支持CLR,即C#代码,你可以尝试用CLR来写。
0

xiaoyaya 2016-06-22 回答

虽然没写过,但是觉得需求挺奇怪,这个你可以写windows脚本来做的吧,为什么非要用SqlServer存储过程呢

要回复问题请先登录注册