Count files in folder

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 939
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Count files in folder

Post by marcoagpinto »

Hello!

Is there a command that shows the total number of files in a folder?

a.txt
b.txt
c.txt

would return: 3

Thanks!
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Count files in folder

Post by #NULL »

Here might be 3 ways: viewtopic.php?f=5&t=47987
ExamineDirectory(), PowerShell, ExplorerListGadget
I don't know how up-to-date they are.
fabulouspaul
User
User
Posts: 34
Joined: Sun Nov 23, 2014 1:18 pm

Re: Count files in folder

Post by fabulouspaul »

I use this little code

Code: Select all

Procedure.i CountFiles(path.s, pattern.s = "*.*", recursive.i = 0)
  Protected dir_handle
  Protected name.s
  Protected counter
  
  If Right(path, 1) <> "\"
    path = path + "\"
  EndIf
    
  dir_handle = ExamineDirectory(#PB_Any, path, pattern)
  
  If dir_handle = 0
    ProcedureReturn 0
  EndIf
  
  While NextDirectoryEntry(dir_handle)
    name = DirectoryEntryName(dir_handle)
    If ReplaceString(name, ".", "") <> "" And DirectoryEntryType(dir_handle) = #PB_DirectoryEntry_Directory And recursive = 1
      counter + CountFiles(path + name + "\", pattern, recursive)
    Else
      counter + 1
    EndIf
  Wend
  
  FinishDirectory(dir_handle)
  
  ProcedureReturn counter
EndProcedure
    
Debug CountFiles("C:\", "*.*", 1)
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Count files in folder

Post by Marc56us »

marcoagpinto wrote:Is there a command that shows the total number of files in a folder?
Windows command (very fast but need some code after)

Code: Select all

RunProgram("cmd", "/k dir | findstr fichier", "C:\Windows", #PB_Program_Wait)

Code: Select all

 58 fichier(s)       14 619 079 octets
Replace "fichier" with your langage
Replace /k with /c and use #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide to use it in code.

:wink:
User avatar
marcoagpinto
Addict
Addict
Posts: 939
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Count files in folder

Post by marcoagpinto »

Thank you, my friends!
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Count files in folder

Post by Dude »

Marc56us wrote:Replace "fichier" with your langage
Here's a better way with "dir", that doesn't need to worry about languages at all. ;)

Returns -1 if the dir doesn't exist, or the number of files in it (but not sub-folders).

Code: Select all

Procedure CountFilesInFolder(dir$)
  If FileSize(dir$)<>-2
    n=-1
  Else
    p=RunProgram(GetEnvironmentVariable("comspec"),"/c dir /b /a-d "+Chr(34)+dir$+Chr(34),"",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
    While ProgramRunning(p)
      If AvailableProgramOutput(p)
        o$+ReadProgramString(p)+#CRLF$
      EndIf
    Wend
    CloseProgram(p)
    n=CountString(o$,#CRLF$)
  EndIf
  ProcedureReturn n
EndProcedure

Debug CountFilesInFolder("C:\Windows")
Debug CountFilesInFolder("C:\Program Files\Internet Explorer\")
Last edited by Dude on Thu Oct 11, 2018 1:43 pm, edited 1 time in total.
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Count files in folder

Post by blueb »

Dude wrote:
Marc56us wrote:Replace "fichier" with your langage
Here's a better way with "dir", that doesn't need to worry about languages at all. ;)

Returns -1 if the dir doesn't exist, or the number of files in it (but not sub-folders).
Sorry Mr. Dude.. code fails if the are spaces in the foldername.
using Win10 Pro and PB 5.70 beta 2 (x86)
- It was too lonely at the top.

System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Count files in folder

Post by Dude »

blueb wrote:code fails if the are spaces in the foldername
Fixed (see edited post). Thanks! ;)
Post Reply