Page 1 of 1

How To Count Files ??

Posted: Mon May 16, 2022 2:15 pm
by Richard_Schmidt
Dear experts,

I need to count the number of files of a specific type in a directory.
For example: number of "*.pdf" or "*.mp3".

Is there a comfortable way to do this ??

I would appreciate any help or idea ..

Thanks in advance
Richard

Re: How To Count Files ??

Posted: Mon May 16, 2022 4:40 pm
by ASESORMIX
Directory function can help you

See: https://vivaclipper.wordpress.com/?s=directory

Re: How To Count Files ??

Posted: Mon May 16, 2022 4:42 pm
by serge_girard
Richard,

Something like this maybe?

Code: Select all

aDIR  := DIRECTORY(  yourFOLDER + '\*.*', '' )
FOR A = 1 TO LEN(aDIR)
   IF '.PDF' $ UPPER(aDIR [A][1] ) 
      // COUNT
   ENDIF
   
NEXT 

Re: How To Count Files ??

Posted: Tue May 17, 2022 1:46 pm
by Algernon
I've used it in aplication and if you only want to know the number of pdf files in a directory I think that it can help you

Code: Select all

aDIR  := DIRECTORY(  yourFOLDER + '\*.pdf', 'D' )

LEN(aDIR)

Re: How To Count Files ??

Posted: Wed May 18, 2022 1:49 pm
by Pcmodula
Hi all, i use this to count multiple type of files

#include "hmg.ch"

FUNCTION Main ()

LOCAL i, cForm

DEFINE WINDOW Win_1 ;
TITLE 'Hello World!' ;
MAIN ;
ON INIT CountMyFiles(/*cPat,aFtype*/)

END WINDOW

cForm := "Win_1"
_DefineHotKey( cForm, 0, VK_ESCAPE, hb_MacroBlock( "_ReleaseWindow('" + cForm + "')" ) )

ACTIVATE WINDOW Win_1

RETURN NIL

*-----------------------------------------------------------------------------*
Static Function CountMyFiles( cPath, eXtSrc )
*-----------------------------------------------------------------------------*
LOCAL i, F , ntc := 0 ,aRtv:={} , cRtv :='' , vGrid
DEFAULT extSrc to {".PDF",".GIF",".TXT",".JPG",".BMP",".PNG",".EXE"}
DEFAULT cPath to cFilePath(GetExeFileName())
Asize ( aRtv, len(eXtSrc) )
Aeval (eXtSrc,{|x,y| Artv[y] :={x,0} } )

vgrid := DIRECTORY( cPath + "\*.*",)
// Put all uppercase
aeval(vGrid, {|x,y| vgrid[y,1] :=upper(x[1]) } )

// Count
FOR Each F IN vGrid
i := ascan(ExtSrc,hb_fnameExt(F[1]) )
IF i > 0
ntc := Artv[i,2] +1
Artv[i,2] := ntc
Endif
NEXT

ASort(aRtv,,, { |x, y| x[2] < y[2] })

// prepare report
FOR Each F in aRtv
if f[2] > 0
Crtv += "Found N° "+hb_ntoc(F[2]) +remleft(F[1],".") +CRLF
Else
Crtv += "Not Found "+remleft(F[1],".") +CRLF
Endif
Next

MsgInfo(Crtv,"Results:")

Return Artv

Saludos
PcModula

Re: How To Count Files ??

Posted: Wed May 18, 2022 4:05 pm
by Richard_Schmidt
Thank you for replies - the hints are very helpful !!
Saludos
Richard