Count files in and folders in directory..

Blitz3D Forums/Blitz3D Programming/Count files in and folders in directory..

Guy Fawkes(Posted 2009) [#1]
Hi all. Can someone tell me how I can count EXACTLY how many files AND folders are in a directory? I dont care if this uses user32 functions.


degac(Posted 2009) [#2]
Hi,

use LoadDir commands.
It returns an array of files (and directory), so you can a 'rough' measurement with array.length or checking any item in it to control the FileType for file only.

' loaddir.bmx

' declare a string array

Local files$[]
Local count:Int,count2:Int
Local _dir:String=CurrentDir()
files=LoadDir(_dir)
For t$=EachIn files
	If FileType(_dir+"/"+t)=1 count:+1
	Print count2+" "+t
	count2:+1
Next
Print "Files: "+count



SabataRH(Posted 2009) [#3]
As this is a Blitz3D forum one would assume he wanted some blitz3d code example, but none the less.

Rez, this function will scan a folder and report the total files and directories found, its recursive so it scans children directories within the given directory. If you don't want it to scan the children directoires just comment out the ScanDir() within the function itself.

Global DirCount, FileCount

ScanDir("C:\temp" )
RuntimeError "Total Dirs: "+DirCount+" || Total Files: "+FileCount


Function ScanDir(folder$)

	myDir=ReadDir(folder$) 

	Repeat 
		file$=NextFile$(myDir) 

		If file$="" Then Exit 
	
		If FileType(folder$+"\"+file$) = 2 Then 
			If file$<>"." And file$<>".." Then
				ScanDir(folder$+"\"+file$)
				DirCount=DirCount+1
			EndIf
		Else 
			FileCount=FileCount+1
		End If 
	Forever 

	CloseDir myDir 

End Function



Guy Fawkes(Posted 2009) [#4]
thanks alot, swampdonkey! works great!


degac(Posted 2009) [#5]
Ops! sorry!