   
;This file traverses the directory tree and computes the total
;size of all files on the drive. 
;
;This code uses two UDF's - user Defined functions. One of them, 
;GetDirSize is a recursive function (it calls itself) and does
;most of the work.
;
;The AddCommas function simply formats the output.
;
;
;NOTE:  This code is just an example.  WinBatch has built in functions
;       (namely DirSize) that can do this operation much faster. 
;

   

;This file traverses the directory tree and computes the total
;size of all files on the C:\ drive. 

;UDF AddCommas to format our final answer for display (only)
#DefineFunction AddCommas(passednumstring)
    ; Grab everything to the left of the decimal (if any)
    intpart=ItemExtract(1,passednumstring, ".")

    ;Grab everything to the right of the decimal
    fractpart=ItemExtract(2,passednumstring,".")

    ;Compute how many comma seperated groups we need
    groups= ((strlen(intpart)-1) / 3 ) +1

    ;Initialize an answer variable to a null string
    answer=""

    ;Figure out what each group looks like
    ;and add each group to the answer variable with
    ;a comma
    for y=1 to groups
       if y==1
          ptr=( (strlen(intpart)-1) mod 3) +1
          answer=strsub(intpart,1, ptr )
          ptr=ptr +1
       else 
          answer=strcat(answer, ",", strsub(intpart, ptr , 3))
          ptr=ptr+3
       endif
    next

    ;Now that we have the numbers to the left of the decimal
    ;point "comma-ized" properly, see if there is any
    ;fractional part to worry about.

    ;If there is a fractional part, glue it onto the end
    ;of he "comma-ized" number along with a decimal
    if fractpart!="" 
       answer=strcat(answer,".",fractpart)
    endif

    ;Return the comma-ized answer
    return (answer)

#endfunction



;Define the function that does all the real work
#DefineFunction GetDirSize(dir)
   
  ;Save original directory
   origdir=DirGet()

   ;Change to directory to inspect
   DirChange(dir)

   ;Display current directory for warm fuzzy feeling
   BoxText(DirGet())

   ;Get total size of files in this directory
   ;and make sure it is a floating point number
   ;by adding 0.0 as it can get very large.
   total=FileSizeEx("*.*")+0.0

   ;Get a list of subdirectories in this directory
   ;and count how many there are.
   dirlist=DirItemize("*.*")
   dircount=ItemCount(dirlist,@tab)

   ;For each subdirectory, call out GetDirSize UDF
   ;to determin its size.
   for xx=1 to dircount
      ;Pick off a subdirectory name
      thisdir=ItemExtract(xx,dirlist,@tab)
      ;Add size of that subdirectory to our running total
      total=total+GetDirSize(thisdir)
   next

   ;Change back to the directory we entered with
   DirChange(origdir)
   
   ;Return current value of total to caller.
   return total
#EndFunction


;True start of program


;Open a Box to display warm fuzzy progress to user
BoxOpen("File Size Inspector","Reading Initial Directories")

;Call our UDF to compute size of C: drive
tot=GetDirSize("C:\")

; Convert to KB
totKB = INT(tot/1024)   

;Display Results
totKB = AddCommas(totKB)
Message("Total File Size of C:\", Strcat(totKB," KB"))
Exit
   
