WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

File Version and Dir Mgmt

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Get size of all subdirectories in a given directory

 Keywords: Get size subdirectories directory  



;*******************************************************************************************************

;** Name:       SubDirSizes

;** Author:     Marc Worrel

;** Version:    1.3    

;** Purpose:    Get size of all subdirectories in a given directory

;** Inputs:     Start directory

;** Outputs:    

;** Revisions:   

;** Date:       01/20/2002

;*******************************************************************************************************

;///////////////////////////////////// SPACER TO SEPARATE SECTIONS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

; Set environmental information and initial variables   |

;-------------------------------------------------------- 

IntControl (12,1+4,0,0,0)                                  ;Suppress OK to close messages

IntControl (49,1,0,0,0)                                    ;Enables Min/Max/Close Widgets

IntControl (1008,1,0,0,0)                                  ;Enables Close command

Result=""

Decimals (2)                                               ;Round to nearest hundredth

abort=@FALSE

Excel=RegExistKey (@REGCLASSES,"Excel.Application")        ;See if Excel is installed

Format=StrCat ("Bytes",@TAB,"Kilobytes",@TAB,"Megabytes",@TAB,"Gigabytes")

;ConfigFile=StrCat(DirGet(),"SubDirSizes.ini")

ConfigFile=ItemReplace("SubDirSizes.ini",ItemCount(IntControl(1004,0,0,0,0),"\"),IntControl(1004,0,0,0,0),"\")

If !FileExist (ConfigFile)

   IniWritePvt ("Settings","StartDir","C:\",ConfigFile)

   IniWritePvt ("Settings","SavePath","C:\DirSizeLog.txt",ConfigFile)

   IniWritePvt ("Settings","Position","201,290,790,700",ConfigFile)

EndIf

StartDir=IniReadPvt ("Settings","StartDir","C:\",ConfigFile)

OutFile =IniReadPvt ("Settings","SavePath","C:\DirSizeLog.txt",ConfigFile)

Position=Arrayize (IniReadPvt("Settings","Position","201,290,790,700",ConfigFile),",")



;*******************************************************************************************************

;///////////////////////////////////// SPACER TO SEPARATE SECTIONS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

;                                                       |

;-------------------------------------------------------- 

#DefineSubRoutine DlgCallback (DlgName,DlgEvent,DlgCtrl,Res4,Res5)

If DlgEvent==0                                             ;Dialog initialization 

   DialogProcOptions (DlgName,02,1)                        ;Watch for button presses

   DialogProcOptions (DlgName,08,1)                        ;Watch for Droplist changes

   DialogProcOptions (DlgName,11,1)                        ;Watch for Close command

   IntControl (1002,0,0,0,0)                               ;Hide WinBatch box

   If !Excel Then DialogControlState (DlgName,003,3,2)     ;If Excel isn't installed, disable the Dump->Excel button 

   ;Disable Maximize widget -- Thanks, Guido!

   hwnd=DllHwnd("Check Directory Sizes")                   ;Window Title of desired window goes here

   sysmenu=DllCall(StrCat(DirWindows(1),"User32.dll"),long:"GetSystemMenu",long:hwnd,long:0)

   DllCall (StrCat(DirWindows(1),"User32.dll"),long:"RemoveMenu",long:sysmenu,long:61488,long:0)   ;Remove maximize from system menu

   OldStyle=DllCall(StrCat(DirWindows(1),"User32.dll"),long:"GetWindowLongA",long:hwnd,long:-16)

   NewStyle=OldStyle & ~65536                              ;Disable Maximize widget

   SetWindow=DllCall(StrCat(DirWindows(1),"User32.dll"),long:"SetWindowLongA",long:hwnd,long:-16,long:NewStyle)

   ;Put window back to the co-ordinates it was at last time it was run

   WinPlace (Position[0],Position[1],Position[2],Position[3],"Check Directory Sizes")

EndIf

If DlgEvent==01 Then

   If DirList <> "" Then

      ThisDir=ItemExtract (1,DirList,@TAB)        

      ThisDirSize=DirSize (StrCat(StartDir,"\",ThisDir),0)*Multiplier

      If StartDirSize<>0                                ;Get percent of parent, avoiding divide by zero errors

         ThisDirPrcnt=(ThisDirSize*100)/StartDirSize    

      Else

         ThisDirPrcnt=0

      EndIf

      If ThisDirPrcnt<100 Then ThisDirPrcnt=StrFixLeft(ThisDirPrcnt,"0",5)  ;Keep percent column right-aligned

      ThisDirSize=StrFixLeft(ThisDirSize," ",10)        ;Keep size column right aligned

      ThisDir=StrFixChars (ThisDir," ",40)              ;Pad name column so sizes line up

      Result=StrCat (Result,ThisDir,ThisDirSize," (",ThisDirPrcnt,"%%)",@TAB)   ;Add subdirectory name and size to result list

      LogResult=StrCat (LogResult,ThisDir,@TAB,ThisDirSize,@TAB,ThisDirPrcnt,@CRLF)   ;Add subdirectory information to list for Excel

      DialogControlSet (DlgName,012,14,StrCat(ThisDir,ThisDirSize," (",ThisDirPrcnt,"%%)"))     ;Add new results to Itembox 

      DialogControlSet (DlgName,012,10,ItemCount(Result,@TAB))         ;Scroll list as new results come in.

      DirList = ItemRemove(1,DirList,@TAB)

    Else

      DialogControlState (DlgName,002,4,2)

      DialogProcOptions (DlgName,1,0)

      DialogControlSet (DlgName,012,5,Result)              ;Display complete results list in Itembox

;      DialogProcOptions (DlgName,1000,0)                   ;Re-enable dialog

      DialogControlState (DlgName,012,4,2)                 ;Re-enable Itembox

    EndIf

EndIf

If (DlgEvent==02 || DlgEvent==08)                          ;CmdButton_Click event or Droplist_Change event

   If DlgCtrl==001                                         ;Browse button pressed

      StartDir=AskDirectory("Select directory to examine","",StartDir,"",0)    ;Browse for new start directory 

      DialogControlSet (DlgName,011,3,StartDir)            ;Update Editbox with new start directory

   EndIf

   If (DlgCtrl==002 || DlgCtrl==010)                       ;Get Size button was pressed or Display Size was changed

;      DialogProcOptions (DlgName,1000,2)                   ;Disable dialog and set Wait cursor

      Format=DialogControlGet (DlgName,010,6)              ;Get size format

      DialogControlState (DlgName,002,3,2)                  ; Disable get size button until finished

      DialogProcOptions (DlgName,1,50)

      If Format=="Bytes" Then Multiplier=1                 

      If Format=="Kilobytes" Then Multiplier=0.0009765625                     

      If Format=="Megabytes" Then Multiplier=0.00000095367431640625           

      If Format=="Gigabytes" Then Multiplier=0.000000000931322574615478515625

      StartDir=DialogControlGet(DlgName,011,3)             ;Get start directory

      If StrIndex (StartDir,"\",0,@BACKSCAN)==StrLen (StartDir) Then StartDir=StrFix (StartDir,"",StrLen(StartDir)-1)

      If !DirExist (StartDir)                              ;Deal with invalid directories

         DialogProcOptions (DlgName,1000,0)                ;Re-enable dialog

         Message ("Invalid Directory","Unable to resolve specified path.")

         DialogControlState (DlgName,011,1,0)              ;Give focus to editbox

         Return (-2)      

      EndIf

      IniWritePvt ("Settings","StartDir",StartDir,ConfigFile)   ;Update Config file with last directory examined

      VolName=DiskVolinfo (ItemExtract(1,StartDir,"\"),1)  ;Get name of volume

      VolSer =DiskVolinfo (ItemExtract(1,StartDir,"\"),3)  ;Get serial number of volume 



      StartDirSize=DirSize (StartDir,0)*Multiplier         ;Get size of parent directory

      DirList=DirItemize (StrCat(StartDir,"\*.*"))         ;Get list of subdirectories

      Result=StrUpper(StrCat(StartDir," (",StartDirSize,"):",@TAB))  ;Reset Results list for next query 

      Result=StrFixLeft (Result," ",StrCharCount(Result)+(290/4-StrCharCount(Result))/2) ;Center parent directory label

      LogResult=StrUpper(StrCat(StartDir," (",StartDirSize,"):",@TAB))   ;Reset LogResults list for next query 

      LogResult=StrCat ("Subdirectory of ",LogResult,"Size:",@TAB,"%% of Parent:",@CRLF)    ;Reset results list formatted for output for next query 

      DialogControlState (DlgName,012,3,2)                 ;Disable Itembox



   EndIf

   If DlgCtrl==003                                         ;Abort Scan button pressed

        Abort=@TRUE

      DialogControlState (DlgName,002,4,2)

      DialogProcOptions (DlgName,1,0)

      DialogControlSet (DlgName,012,5,Result)              ;Display complete results list in Itembox

;      DialogProcOptions (DlgName,1000,0)                   ;Re-enable dialog

      DialogControlState (DlgName,012,4,2) 

   EndIf

   If DlgCtrl==004                                         ;Dump to Excel button pressed

      If DialogControlGet (DlgName,012,5)=="" Then Return (-2)  ;If no query has been run yet, don't try to show results

      ClipPut (LogResult)                                  ;Put the results in the clipboard

      If WinExist("~Excel") 

         SendKeysTo ("~Excel","^n")                        ;If Excel is already in use, open new spreadsheet

      Else

         ErrorMode (@OFF)                                  ;Tell WinBatch to allow errors in case Excel is not found

         If Run ("Excel","")==@FALSE                       ;Open Excel

            ErrorMode (@CANCEL)                            ;Resume halting on errors

            Message ("Invalid destination","Excel must be in the system path to receive output")

            Return (-2)

         EndIf  

         ErrorMode (@CANCEL)                               ;Resume halting on errors 

      EndIf

      SendKeysTo ("~Excel","^v")                           ;Paste into Excel.  I know--this is a very ugly way to to it      

   EndIf

   If DlgCtrl==005                                         ;Dump to Text button pressed

      If DialogControlGet (DlgName,012,5)=="" Then Return (-2)  ;If no query has been run yet, don't try to log results

      Result=StrReplace (DialogControlGet(DlgName,012,5),@TAB,@CRLF)

      OutBuf=BinaryAlloc (StrLen(Result)+10)

      BinaryPokeStr (OutBuf,0,Result)

      OutFile=AskFilename ("Save As",FilePath(OutFile),"Text Files|*.txt|All Files|*.*|",ItemExtract(-1,OutFile,"\"),0)

      IniWritePvt ("Settings","SavePath",OutFile,ConfigFile)    ;Update Config file with last path for log file

      BinaryWrite (OutBuf,OutFile)

      BinaryFree (OutBuf)

   EndIf

   If DlgCtrl==006                                         ;About button pressed

        AboutMsg=StrCat ("SubDirSizes",@CRLF,@CRLF,"Version 1.3",@CRLF,@CRLF,"Written by Marc Worrel")

        AboutMsg=StrCat (AboutMsg," with the assistance of",@CRLF,"the WinBatch forum and lots of Moutain Dew.")

        Message ("About SubDirSizes",AboutMsg)

   EndIf

   If DlgCtrl==007                                         ;Exit button pressed 

      IniWritePvt ("Settings","Position",WinPosition("Check Directory Sizes"),ConfigFile)

      Exit

   EndIf

EndIf

If DlgEvent==11                                            ;Close widget pressed

   IniWritePvt ("Settings","Position",WinPosition("Check Directory Sizes"),ConfigFile)

   Exit

EndIf

:CANCEL                                                    ;If Cancel button is pressed in Ask box, return to dialog

Return (-2)                                                ;Return (-1) for normal processing or (-2) for persistant dialog

#EndSubRoutine



DirSizesFormat=`WWWDLGED,6.1`

DirSizesCaption=`Check Directory Sizes`

DirSizesX=-100

DirSizesY=100

DirSizesWidth=302

DirSizesHeight=148

DirSizesNumControls=012

DirSizesProcedure=`DlgCallback`

DirSizesFont=`DEFAULT`

DirSizesTextColor=`DEFAULT`

DirSizesBackground=`DEFAULT,DEFAULT`

DirSizesConfig=1

DirSizes001=`258,005,036,012,PUSHBUTTON,DEFAULT,"&Browse",2,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

DirSizes002=`006,127,036,012,PUSHBUTTON,DEFAULT,"&Get Size",1,5,32,DEFAULT,DEFAULT,DEFAULT`

DirSizes003=`048,127,036,012,PUSHBUTTON,DEFAULT,"A&bort Scan",6,6,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

DirSizes004=`090,127,036,012,PUSHBUTTON,DEFAULT,"Dump->&Excel",3,7,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

DirSizes005=`132,127,036,012,PUSHBUTTON,DEFAULT,"Dump->&Text",4,8,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

DirSizes006=`216,127,036,012,PUSHBUTTON,DEFAULT,"&About",5,9,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

DirSizes007=`258,127,036,012,PUSHBUTTON,DEFAULT,"E&xit",0,10,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

DirSizes008=`007,007,040,008,STATICTEXT,DEFAULT,"&Display Sizes In:",DEFAULT,11,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

DirSizes009=`106,007,022,008,STATICTEXT,DEFAULT,"&Start Dir:",DEFAULT,1,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

DirSizes010=`050,005,040,050,DROPLISTBOX,Format,"Megabytes",DEFAULT,12,4,DEFAULT,DEFAULT,DEFAULT`

DirSizes011=`131,005,122,012,EDITBOX,StartDir,DEFAULT,DEFAULT,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

DirSizes012=`004,021,291,102,ITEMBOX,Result,DEFAULT,DEFAULT,4,DEFAULT,"Courier|5632|40|49","0|0|0",DEFAULT`

DirSizesButtonPushed=Dialog ("DirSizes",1)



Exit

Article ID:   W15697
File Created: 2003:05:13:11:29:38
Last Updated: 2003:05:13:11:29:38