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.

Recursive File Size Example

Keywords: file size  filesize dirsize 

Question:

I'm trying to figure out a way of determining the total size of all files in a directory tree. I would want to specify a "starting" point in the directory tree and then have the program add up all file sizes below the starting point. Any ideas?

Answer:

Take a look at the DirSize function, available starting in Winbatch 98A.

Otherwise, a user created a program to do this.

We have a directory F:\USERS and under the users directory is a dir for each user. The following code creates a report to give me the total used by each person in their own directory.

;**************************************************************
; This program is used for LAN admins to see who is 'hogging' *
; disk space.  Future versions could scan for certain criteria*
; such as Dirs named "DOOM" or SETUP.EXE or *.??_ files.      *
;**************************************************************

; Setup variables
level = 0
flag = 0
user_total = 0.0	 ; note you need the floating point number here
total_total = 0
search_users = ""
xCRLF = strcat(@CRLF,@CRLF)

; --- This includes SYS and HIDDEN files.
intcontrol(5,1,0,0,0) 


:check_dialog
gosub Sub_setup
If Direxist(start_path) == @FALSE 
	Pause("ERROR","Drive/Path is invalid")
	goto check_dialog
endif

DirChange(start_path)

if Mail == 1 && mail_name == "" 
	Pause("ERROR","Mail name is invalid")
	goto check_dialog
endif

if output == 1 
	fhandle = fileopen("%output_file%","WRITE")
endif 

;******************************************
; Call subdirs for which users to process *
;******************************************

Switch Rpt_type
	case 1
		gosub Sub_oneuser  ;To select only 1 user
	break
	case 2
		gosub Sub_exclude  ;To exclude 1 or more users
	break
	case 3
		gosub Sub_allusers ;To select all users
	break
EndSwitch
;--------------------------------------------------------------------

dirs%level%=ItemSort(dirs%level%,@TAB)

BoxOpen("Processing User Directories", "")

;------------- Write heading to file, if output is ON
if output == 1 
	Filewrite(fhandle,"Directory analyzer")
	Filewrite(fhandle,"") ; blank line
	Filewrite(fhandle,"Dir Name      Size")
	Filewrite(fhandle,"-----------------------")
	Filewrite(fhandle,"") ; blank line
endif

:LOOP1

;----- Get current directory info

cur_dir%level%=itemextract(1,dirs%level%,@TAB)

dirs%level%=itemremove(1,dirs%level%,@TAB)
num_dirs%level%=itemcount(dirs%level%,@TAB)
dirchange(cur_dir%level%) 

dirname=cur_dir%level%
num2fmt=user_total
gosub Sub_numformat
BoxText("             User:  %cur_dir0%%xCRLF%      Directory:  %dirname%%xCRLF%Current Total:  %formattednum%")
 
level=level+1
dirs%level%=diritemize("*.*")
num_dirs%level%=itemcount(dirs%level%,@TAB)

if num_dirs%level% > 0 then goto LOOP1

if !num_dirs%level% > 0 
	fs = filesize("*.*")	

	gosub Sub_WriteDetail		

	user_total=user_total + fs
	level = level-1
	dirchange("..")
	dirname=cur_dir%level%
	num2fmt=user_total
	gosub Sub_numformat
	BoxText("             User:  %cur_dir0%%xCRLF%      Directory:  %dirname%%xCRLF%Current Total:  %formattednum%")

	while num_dirs%level% == 0 && level > 0
		fs = filesize("*.*")
		gosub Sub_WriteDetail
		user_total=user_total + fs
		level = level -1
		dirchange("..")
		dirname=cur_dir%level%
		num2fmt=user_total
		gosub Sub_numformat
		BoxText("             User:  %cur_dir0%%xCRLF%      Directory:  %dirname%%xCRLF%Current Total:  %formattednum%")
	end while

	if level == 0 
		cur_dir0 = strfixchars(cur_dir0," ",12)
		num2fmt=user_total
		gosub Sub_numformat
		if 1 == 1 
			FileWrite(fhandle,"---------------------------")
		endif
		FileWrite(fhandle,"%cur_dir0% Total:  %formattednum% ")
		FileWrite(fhandle,"")
		user_total=0
		if num_dirs0 == 0 then flag= 1
	endif
endif

if flag == 0 then goto loop1

BoxShut()

exit()
		
;--------------------------------------------------------------------
;************************************************
;* All subroutines used are			*
;* listed below.				*
;************************************************

;*********************************************************
; Write Detail						 *
; Checks if DETAIL is turned on, and writes to the output*
; file.	 Will flag those dirs over xx in size.		 *
;*********************************************************
:Sub_WriteDetail
	if 1==1				; --- If details are turned on!
		full_name=dirget()
		if fs < 2000000
			FileWrite(fhandle,"               %full_name%%tempname%   %fs%")	
		else
			FileWrite(fhandle,"        ***    %full_name%%tempname%   %fs%")	
	endif
return


;*********************************************************
; Format Numbers					 *
; Arguments: num2format = number to be formatted.	 *
; Returns:   formattednum = formatted number with commas.*
;*********************************************************
:Sub_numformat
formattednum=""
length=strlen(num2fmt)
orglength=length
for x = 1 to orglength
	y=strsub(num2fmt,length,1)
	formattednum=StrCat(y,formattednum)
	length=length-1
	if x mod 3 == 0 && x != orglength then formattednum=StrCat(",",formattednum)
next x
return

;********************************
; This section allows the user	*
; to exclude certain dirs from	*
; the scanning/reporting.	*
;********************************
:Sub_exclude
dirs%level%=diritemize("*.*")
search_users=askitemlist("Pick users to exclude.",dirs%level%,@TAB,@SORTED,@MULTIPLE)
item_count = itemcount(search_users,@TAB)
for loop = 1 to item_count
	rem_item = itemextract(1,search_users,@TAB)
	search_users=itemremove(1,search_users,@TAB)
	x=itemlocate(rem_item,dirs%level%,@TAB)
	dirs%level% = itemremove(x,dirs%level%,@TAB)
next loop
return

:Sub_oneuser
search_users=diritemize("*.*")
search_users=askitemlist("Pick user to report.",search_users,@TAB,@SORTED,@SINGLE)
dirs%level%=search_users
item_count = 1
return

:Sub_allusers
dirs%level%=diritemize("*.*")
return

:Sub_setup
MyDialogFormat=`WWWDLGED,5.0`

MyDialogCaption=`Disk Hog`
MyDialogX=68
MyDialogY=87
MyDialogWidth=177
MyDialogHeight=145
MyDialogNumControls=12

MyDialog01=`54,2,78,DEFAULT,STATICTEXT,DEFAULT,"Disk Hog Configuration"`
MyDialog02=`54,14,78,DEFAULT,STATICTEXT,DEFAULT,"--------------------------------------"`
MyDialog03=`18,34,38,DEFAULT,STATICTEXT,DEFAULT,"Root Path:"`
MyDialog04=`66,34,108,DEFAULT,EDITBOX,pathname,"F:\USERS"`
MyDialog05=`4,58,50,DEFAULT,RADIOBUTTON,Rpt_type,"Single User",1`
MyDialog06=`64,58,56,DEFAULT,RADIOBUTTON,Rpt_type,"Exclude User",2`
MyDialog07=`130,58,42,DEFAULT,RADIOBUTTON,Rpt_type,"All Users",3`
MyDialog08=`2,84,54,DEFAULT,CHECKBOX,Mail,"Mail Report?",1`
MyDialog09=`66,84,108,DEFAULT,EDITBOX,mail_name,""`
MyDialog10=`2,108,60,DEFAULT,CHECKBOX,output,"Output to File?",1`
MyDialog11=`66,108,108,DEFAULT,EDITBOX,output_file,"C:\TEMP\DISKHOG.RPT"`
MyDialog12=`66,130,40,DEFAULT,PUSHBUTTON,DEFAULT,"OK",1`


Rpt_type=3
Mail=0
output=1
ButtonPushed=Dialog("MyDialog")
start_path = pathname

return

Article ID:   W13793
Filename:   Recursive FileSize Example.txt
File Created: 2000:12:26:14:11:22
Last Updated: 2000:12:26:14:11:22