标题: [原创] [分享] getFoldersSize.vbs 枚举子目录大小 [打印本页]
作者: CrLf 时间: 2013-8-4 17:56 标题: [分享] getFoldersSize.vbs 枚举子目录大小
本帖最后由 CrLf 于 2013-8-4 18:01 编辑
也来凑热闹,vbs 用 getfolder.size 简单实现枚举目录大小,效率上应该是慢于 vbs 中递归求和的,但不会因为文件夹权限限制而漏算(有碰到过权限不足进不去的情况):- Dim arg,ws '对象
- Dim Mode_SubFolders '布尔
- Dim path '字符串
-
- Set arg = WScript.Arguments
- path = "." '遍历路径默认为当前目录
- If arg.Unnamed.Count Then path = arg.Unnamed(0) '获取遍历路径
- Mode_SubFolders = arg.Named.Exists("s") '判断是否使用了 /s 开关,不分大小写
-
- getSize(path) '调用过程
-
- '函数 & 过程
- Sub getSize(ByVal path)
- On Error Resume Next '忽略错误
- Dim fso
- Set fso = WScript.CreateObject("Scripting.Filesystemobject")
-
- For Each subFolder In fso.GetFolder(path).SubFolders
- WScript.Echo subfolder.Size&" " & subfolder '
- If Mode_SubFolders Then getSize(subFolder) '为子目录调用过程
- Next
- End Sub
复制代码
调用方法:- cscript getFoldersSize.vbs d:
- ::获取 D:\ 路径下的所有目录大小,不含子目录
- cscript getFoldersSize.vbs /s
- ::获取当前工作路径下的所有子目录大小
- cscript getFoldersSize.vbs /s c:\windows
- ::获取 c:\windows 路径下的所有子目录大小
复制代码
作者: 523066680 时间: 2013-8-4 23:02
楼主有空的话增加个可选参数 - 以MB为单位显示大小
到时候拿来用
作者: CrLf 时间: 2013-8-5 01:18
本帖最后由 CrLf 于 2013-8-5 02:12 编辑
来一个递归求和的方案,求快就不提供开关了:- Dim fso,arg '对象
- Dim path '字符串
-
- Set fso = WScript.CreateObject("Scripting.Filesystemobject")
- Set arg = WScript.Arguments
-
- path = "." '遍历路径默认为当前目录
- If arg.Unnamed.Count Then path = arg.Unnamed(0) '获取遍历路径
-
- WScript.Echo getFolderSize(path) + getFileSize(path) + 0 & " " & fso.GetFolder(path).Path '调用过程
-
- '函数 & 过程
- Function getFolderSize(ByVal path)
- On Error Resume Next '忽略错误
- Dim folderSize '定义临时变量
-
- For Each folder In fso.GetFolder(path).SubFolders
- folderSize = getFolderSize(folder) + getFileSize(folder)
- WScript.Echo folderSize + fileSize + 0 & " """ & folder.Path & """" '为子目录调用过程
- getFolderSize = getFolderSize + folderSize '获取子目录体积总和
- Next
- End Function
-
- Function getFileSize(ByVal path)
- On Error Resume Next '忽略错误
-
- For Each file In fso.GetFolder(path).Files
- getFileSize = getFileSize + file.Size '获取子文件体积总和
- Next
- End Function
复制代码
用法是- cscript /nologo getFoldersSize.vbs d:
复制代码
试了下很惊奇地发现这个居然没有顶楼 getfolder.size 的方案快
作者: freesoft00 时间: 2015-5-27 21:10
回复 1# CrLf
win7 64位我测试出现无法找到脚本文件“F:\33.vbs”的错误提示。
我把vbs脚本改为了33.vbs
作者: CrLf 时间: 2018-7-11 11:48
本帖最后由 CrLf 于 2018-7-11 12:39 编辑
正好用到,用 bat+js 重写了一个- 1>1/* :
- @cscript /nologo /e:jscript "%~f0" %*
- @exit /b
- */
-
- var arg = WScript.Arguments
-
-
-
- var option = {
- SubFolders: arg.Named.Exists("s"),
- Help: arg.Named.Exists("?")
- }
-
- if(option.Help){
- WSH.Echo(
- 'getFoldersSize 批量获取文件夹大小 v1.1 by http://bbs.bathome.net CrLf\r\n'
- + '\r\n'
- + 'Usage: getFoldersSize [Path] [/S]\r\n'
- + '\r\n'
- + ' Path 目标文件夹路径或目录名,目录名可包含通配符\r\n'
- + '\r\n'
- + ' /S 遍历子目录\r\n'
- )
-
- WSH.Quit()
- }
-
-
- var strPath = "."
- var rePattern = new RegExp()
-
- if(arg.Unnamed.Count){
- var strFoler = arg.Unnamed(0)
-
- strPath = strFoler.replace(/[^\\\/:]+$/,'') || '.'
-
- rePattern.compile(
- '^'
- +strFoler
- .replace(/.*[\\\/:\|"'\<\>]/g,'')
- .replace(/\W/g,'\\$&')
- .replace(/\\\*/g,'.*')
- .replace(/\\\?/g,'.')
- +'$'
- ,'im'
- )
- }
-
- getFolderSize(strPath, rePattern, option)
-
-
-
- function getFolderSize(strPath, rePattern, option){
-
- var fso = WScript.CreateObject("Scripting.Filesystemobject")
-
- var objFolder = fso.GetFolder(strPath);
- var objSubFolders = new Enumerator(objFolder.SubFolders);
-
- for (; !objSubFolders.atEnd(); objSubFolders.moveNext()) {
- var fc = objSubFolders.item()
- try{
- if(fc.Name.search(rePattern)>-1){
- var size = (' '+fc.Size).replace(/.*(.{12})$/,'$1')
- var name = option.SubFolders ? fc.Path + '\\' + fc.Name : fc.Name
- WSH.Echo(size+' '+name);
- }
- if(option.SubFolders)getSubFolderSize(fc, rePattern, option)
- }
- catch(E){
- WSH.StdErr.WriteLine('Error:'+E.message+' - '+fc.Name);
- }
- }
- }
-
- function getSubFolderSize(objFolder, rePattern, option){
- var objSubFolders = new Enumerator(objFolder.SubFolders);
- var arrEcho = []
- for (; !objSubFolders.atEnd(); objSubFolders.moveNext()) {
- var fc = objSubFolders.item()
- try{
- if(fc.Name.search(rePattern)>-1){
- var size = (' '+fc.Size).replace(/.*(.{12})$/,'$1')
- var name = fc.Path + '\\' + fc.Name
- WSH.Echo(size+' '+name);
- }
- }
- catch(E){
- WSH.StdErr.WriteLine('Error:'+E.message+' - '+fc.Name);
- }
-
- getSubFolderSize(fc, rePattern, option)
- }
- }
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |