home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 March / Chip_2002-03_cd1.bin / sharewar / winbatch / winbatch.exe / filesize.wb_ < prev    next >
Encoding:
Text File  |  2001-05-04  |  3.3 KB  |  124 lines

  1.    
  2. ;This file traverses the directory tree and computes the total
  3. ;size of all files on the drive. 
  4. ;
  5. ;This code uses two UDF's - user Defined functions. One of them, 
  6. ;GetDirSize is a recursive function (it calls itself) and does
  7. ;most of the work.
  8. ;
  9. ;The AddCommas function simply formats the output.
  10. ;
  11. ;
  12. ;NOTE:  This code is just an example.  WinBatch has built in functions
  13. ;       (namely DirSize) that can do this operation much faster. 
  14. ;
  15.  
  16.    
  17.  
  18. ;This file traverses the directory tree and computes the total
  19. ;size of all files on the C:\ drive. 
  20.  
  21. ;UDF AddCommas to format our final answer for display (only)
  22. #DefineFunction AddCommas(passednumstring)
  23.     ; Grab everything to the left of the decimal (if any)
  24.     intpart=ItemExtract(1,passednumstring, ".")
  25.  
  26.     ;Grab everything to the right of the decimal
  27.     fractpart=ItemExtract(2,passednumstring,".")
  28.  
  29.     ;Compute how many comma seperated groups we need
  30.     groups= ((strlen(intpart)-1) / 3 ) +1
  31.  
  32.     ;Initialize an answer variable to a null string
  33.     answer=""
  34.  
  35.     ;Figure out what each group looks like
  36.     ;and add each group to the answer variable with
  37.     ;a comma
  38.     for y=1 to groups
  39.        if y==1
  40.           ptr=( (strlen(intpart)-1) mod 3) +1
  41.           answer=strsub(intpart,1, ptr )
  42.           ptr=ptr +1
  43.        else 
  44.           answer=strcat(answer, ",", strsub(intpart, ptr , 3))
  45.           ptr=ptr+3
  46.        endif
  47.     next
  48.  
  49.     ;Now that we have the numbers to the left of the decimal
  50.     ;point "comma-ized" properly, see if there is any
  51.     ;fractional part to worry about.
  52.  
  53.     ;If there is a fractional part, glue it onto the end
  54.     ;of he "comma-ized" number along with a decimal
  55.     if fractpart!="" 
  56.        answer=strcat(answer,".",fractpart)
  57.     endif
  58.  
  59.     ;Return the comma-ized answer
  60.     return (answer)
  61.  
  62. #endfunction
  63.  
  64.  
  65.  
  66. ;Define the function that does all the real work
  67. #DefineFunction GetDirSize(dir)
  68.    
  69.   ;Save original directory
  70.    origdir=DirGet()
  71.  
  72.    ;Change to directory to inspect
  73.    DirChange(dir)
  74.  
  75.    ;Display current directory for warm fuzzy feeling
  76.    BoxText(DirGet())
  77.  
  78.    ;Get total size of files in this directory
  79.    ;and make sure it is a floating point number
  80.    ;by adding 0.0 as it can get very large.
  81.    total=FileSizeEx("*.*")+0.0
  82.  
  83.    ;Get a list of subdirectories in this directory
  84.    ;and count how many there are.
  85.    dirlist=DirItemize("*.*")
  86.    dircount=ItemCount(dirlist,@tab)
  87.  
  88.    ;For each subdirectory, call out GetDirSize UDF
  89.    ;to determin its size.
  90.    for xx=1 to dircount
  91.       ;Pick off a subdirectory name
  92.       thisdir=ItemExtract(xx,dirlist,@tab)
  93.       ;Add size of that subdirectory to our running total
  94.       total=total+GetDirSize(thisdir)
  95.    next
  96.  
  97.    ;Change back to the directory we entered with
  98.    DirChange(origdir)
  99.    
  100.    ;Return current value of total to caller.
  101.    return total
  102. #EndFunction
  103.  
  104.  
  105. ;True start of program
  106.  
  107. ;Allow system and hidden files to be counted
  108. IntControl(5,1,0,0,0)      
  109.  
  110. ;Open a Box to display warm fuzzy progress to user
  111. BoxOpen("File Size Inspector","Reading Initial Directories")
  112.  
  113. ;Call our UDF to compute size of C: drive
  114. tot=GetDirSize("C:\")
  115.  
  116. ; Convert to KB
  117. totKB = INT(tot/1024)   
  118.  
  119. ;Display Results
  120. totKB = AddCommas(totKB)
  121. Message("Total File Size of C:\", Strcat(totKB," KB"))
  122. Exit
  123.    
  124.