ClaudeLachapelle
Thu Nov 29 14:54:05 PST 2007
Beautiful!
Thanks a lot Tom!
"Tom Lavedas" wrote:
> On Nov 29, 11:26 am, "rusga" <only@newsgroup> wrote:
> > Would you be so kind to post me when you get an answer please? Thank you.
> >
> > rusga
> > Rockstar
> >
> > "Claude Lachapelle" <ClaudeLachape...@discussions.microsoft.com> wrote in
> > messagenews:F79D03A2-AF42-4A3B-8EFD-A8D65BA00F3B@microsoft.com...
> >
> > > Still unanswered -- any way in VBScript to get this information
> > > (getfoldersize)?
> >
> > > Thanks.
> >
> > > Claude Lachapelle
> > > Systems Administrator, MCSE
>
> The reason there has been so much silence on this subject is that
> there is no easy way to get this information in script. Not even WMI
> provides a way to determine this data. My searches turned up a number
> of references to a system API call, which is tough to do in script, or
> a suggestion to compute the number from file and allocation unit
> sizes, but without a specific way to do it.
>
> I'm not into API calls, though there are some third party applications
> that make it possible, I guess - oh, and I remember one example of
> doing it using VBA in an Office application, like Excel. But that
> seems like using a sludge hammer to swat a fly, both unwieldy and
> overkill.
>
> So, I approached the problem as a puzzle to be solved using the
> computation approach. To do this, I needed the compressed file size
> and the allocation unit size. Neither is easy to come by. I finally
> found an intrinsic command line utility (in XP at least) called
> COMPACT.exe that returns the compacted file size, but can not think of
> a practical means of determining the allocation unit size. It can be
> had from the CHKDSK utility for a floppy (what are those, you ask) or
> hard drive, but it is extremely slow for hard drives and can't be
> applied to some other media, such as a CD. So, I am going to leave
> that to others. I just assume the standard 4096 byte cluster size of
> a common NTFS disk installation in the solution shown below ...
>
> '-------------------8<------------------
> Const AllocationUnitSize = 4096 ' bytes
>
> sFileSpec = ".\*" ' current folder, for example
>
> sCmd = "compact " & sFileSpec & " | find "" = """
> aFiles = CmdPrompt(sCmd)
> nSizeonDisk = 0
> For each anItem in aFiles
> if trim(anItem) <> "" then
> nFileSize = Split(Replace(Replace(anItem, " ", "")_
> ,"=", ":"), ":")(1)
> nUnits = nFileSize \ AllocationUnitSize
> if nFileSize Mod AllocationUnitSize > 0 then _
> nUnits = nUnits + 1
> nSizeonDisk = nSizeonDisk _
> + nUnits * AllocationUnitSize
> End If
> next
> wsh.echo "Size on Disk:", nSizeonDisk
>
> Function CmdPrompt(sCmd)
> Dim alines, sCmdLine, stemp, ofs, oWS, nRes
> 'On Error Resume Next
> sCmdLine = """%comspec%"" /c " & sCmd & " >> "
> set ofs = CreateObject("Scripting.FileSystemObject")
> stemp = ofs.GetTempName
> set oWS = CreateObject("Wscript.Shell")
> stemp = oWS.Environment("PROCESS")("TEMP") & "\" & stemp
> nRes = oWS.Run(sCmdLine & Chr(34) & sTemp & Chr(34) & " 2>>&1" _
> , 0, True)
> 'alines = "ERRORLEVEL: " & nRes & vbCRLF
> if ofs.FileExists(sTemp) Then
> with ofs.OpenTextFile(stemp)
> if Not .AtEndofStream Then
> alines = aLines & .ReadAll
> End if
> End With
> ofs.DeleteFile stemp
> alines = Split(aLines, vbNewline)
> Else
> aLines = Array(nRes, "")
> End if
> ReDim Preserve alines(Ubound(alines) - 1)
> if Err.Number <> 0 Then _
> aLines = Array("Error Number:" & CStr(Err.Number),
> Err.Description)
> CmdPrompt = alines
> End Function
> '-------------------8<------------------
>
> A file name or wildcard filespec is provided for the search as well as
> an allocation unit size, if it differs from the standard I used. For
> example, to size the contents of a folder named 'c:\Test', the
> filespec would be 'c:\Test\*'.
>
> My quick tests gave the same results as the folder Properties 'Size on
> disk' in explorer for a couple of compressed folder and an
> uncompressed one, as well.
>
> I hope this serves. It's the best I could figure out.
>
> Tom Lavedas
> ===========
>
http://members.cox.net/tglbatch/wsh/
>