【VBS】获取文件夹大小

时间:2024-09-05 12:34:50

文件截图:

【VBS】获取文件夹大小

运行结果:

【VBS】获取文件夹大小

第一步:编写脚本 GetFloderSize.vbs

 1 '获得文件夹的大小  by  王牌飞行员(https://www.cnblogs.com/KMould/p/12334814.html)  2020年02月20日
2 Option Explicit
3 On Error Resume Next
4 Dim objFSO, objLocalFolder, strArg, longLocalFolderSize, strSizeMess
5 '判断参数
6 If WScript.Arguments.Count < 2 Then
7 WScript.Echo "缺少参数,第一个参数为文件夹路径,第二个参数为报告路径"
8 WScript.Quit
9 Else
10 strArg = WScript.Arguments(0)
11 End If
12
13 Set objFSO = CreateObject("Scripting.FileSystemObject")
14 Set objLocalFolder = objFSO.GetFolder(strArg)
15
16 If objLocalFolder = Empty Then
17 WScript.Echo "Invalid Path"
18 WScript.Quit
19 End If
20
21 longLocalFolderSize = objLocalFolder.Size
22
23 If longLocalFolderSize>=1024 And longLocalFolderSize<1024*1024 Then
24 strSizeMess = Round( longLocalFolderSize/1024, 3 ) & " K"
25 ElseIf longLocalFolderSize>=1024*1024 And longLocalFolderSize<1024*1024*1024 Then
26 strSizeMess = Round( longLocalFolderSize/1024/1024, 3 ) & " M"
27 ElseIf longLocalFolderSize>=1024*1024*1024 And longLocalFolderSize<1024*1024*1024*1024 Then
28 strSizeMess = Round( longLocalFolderSize/1024/1024/1024, 3 ) & " G"
29 ElseIf longLocalFolderSize>=1024*1024*1024*1024 Then
30 strSizeMess = Round( longLocalFolderSize/1024/1024/1024/1024, 3 ) & " T"
31 Else
32 strSizeMess = longLocalFolderSize & " B"
33 End If
34 Set objFSO = Nothing
35 Set objLocalFolder = Nothing
36
37 '-------------------get report --------------------------------
38 Dim strReport,fs,f
39 strReport=WScript.Arguments(1)
40 set fs =CreateObject("scripting.filesystemobject")
41 set f =fs.opentextfile(strReport,8)
42 f.writeline date() & " " & strArg & " " & strSizeMess
43 f.close
44 Set fs = Nothing
45 '--------------------------------------------------------------
46 'WScript.Echo "完成!"
47 WScript.Quit

第二步:使用批处理脚本GetFloderSize.bat调用VBS脚本使用

@echo off
rem 文件夹路径 报告路径
call GetFloderSize.vbs D:\M D:\报告.txt
echo 成功!