'on error resume next Dim bRecurse, sFolder, oOutputFile bRecurse=False Dim iLevel Dim iCount iLevel=0 iCount=0 if instr(lcase(wscript.fullname),"wscript.exe")>0 then 'msgbox "Please run from a command prompt with cscript (i.e. cscript getsize.vbs)." 'constants from C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\shlobj.h iRC=msgbox("Recurse subdirectories?",259,"Get Size") if iRC=6 then bRecurse=True elseif iRC=7 then bRecurse=False else wscript.quit'iRC=2 end if sFolder=GetFolderGUI if len(sFolder) < 1 then wscript.quit elseif wscript.arguments.count < 1 or wscript.arguments.count > 2 then usage elseif wscript.arguments.count = 1 then sFolder=wscript.arguments(0) if instr(sFolder,"?") then usage else sFolder=wscript.arguments(0) if lcase(wscript.arguments(1))="/s" then bRecurse=True end if set oFS=wscript.CreateObject("scripting.filesystemobject") set oTopFolder=oFS.GetFolder(sFolder) sOutputFile=GetCurrentDir&"GetSize.csv" set oOutputFile=oFS.createtextfile(sOutputFile,True) oOutputFile.writeline "Count,Recursion Level,Name,Full Path,Size (in bytes)" call GetSize (oTopFolder) oOutputFile.close set oWSHShell=createobject("wscript.shell") oWSHShell.run "explorer """&sOutputFile&"""",5,False Sub GetSize(oCurFolder) iCount=iCount+1 'wscript.echo iCount&","""&String(iLevel,"_")&oCurFolder.name&""","""&oCurFolder.Path&""","""&commas(oCurFolder.size)&"""" iSize="" 'on error resume next err.clear iSize=oCurFolder.size if err.number<>0 then iSize="Error 0x"&cstr(hex(err.number))&" "&err.description err.clear else iSize=commas(iSize) end if sCFName="" sCFName=oCurFolder.Name if err.number<>0 then sCFName="Error 0x"&cstr(hex(err.number))&" "&err.description err.clear end if sCFPath="" sCFPath=oCurFolder.Path if err.number<>0 then sCFPath="Error 0x"&cstr(hex(err.number))&" "&err.description err.clear end if 'on error goto 0 oOutputFile.writeline iCount&","&iLevel&","""&sCFName&""","""&sCFPath&""","""&commas(iSize)&"""" err.clear if err.number = 0 then if bRecurse then if oCurFolder.SubFolders.Count > 0 then iLevel=iLevel+1 for each oSubFolder in oCurFolder.SubFolders if err.number = 0 then if iLevel=1 or bRecurse then call GetSize (oSubFolder) else err.clear end if next if oCurFolder.SubFolders.Count > 0 then iLevel=iLevel-1 end if else err.clear end if End Sub Function Commas(sNumber) Commas=sNumber on error resume next if len(sNumber) > 0 then if cdbl(sNumber) > 0 then Commas=FormatNumber(sNumber,0,0,-1) end if end if End Function Function GetFolderGUI() BIF_NEWDIALOGSTYLE = &h40 BIF_EDITBOX = &h10 BIF_NONEWFOLDERBUTTON = &h200 BIF_SHAREABLE = &h8000 BIF_VALIDATE = &h20 BIF_EDITBOX = &h10 BIF_STATUSTEXT = &h4 BIF_USENEWUI=BIF_NEWDIALOGSTYLE Or BIF_EDITBOX or BIF_VALIDATE or BIF_SHAREABLE set oShell=createobject("Shell.Application") 'See ShellSpecialFolderConstants for third parameter optinos set oFolder=oShell.BrowseForFolder(0,"Please choose a folder",BIF_USENEWUI,0) 'set oFolder=oShell.BrowseForFolder(0,"Please choose a folder",BIF_USENEWUI,"c:\") if len(TypeName(oFolder)) < 6 then exit function end if if lcase(left(TypeName(oFolder),6))<>"folder" then exit function end if set oFolderItem=oFolder.Items.Item GetFolderGUI=oFolderItem.Path End Function Function GetCurrentDir GetCurrentDir=left(wscript.scriptfullname,instrrev(wscript.scriptfullname,"\")) End Function Sub Usage wscript.echo "Usage:" wscript.echo wscript.echo "cscript getsize.vbs ""path"" [/s]" wscript.echo wscript.echo "specify ""/s"" to recurse directory structure" wscript.echo "or run with wscript for gui." wscript.echo wscript.echo "i.e.: cscript //nologo getsize ""d:\program files"" /s > myfile.csv" wscript.echo " cscript //nologo getsize ""\\myserver\c$\program files"" > myfile.csv" wscript.echo wscript.quit End Sub