It is currently Tue Oct 23, 2018 8:36 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Count files in folder
PostPosted: Thu Oct 11, 2018 9:54 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Mar 10, 2013 3:01 pm
Posts: 562
Location: Portugal
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: Count files in folder
PostPosted: Thu Oct 11, 2018 10:01 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 811
Location: right here
Here might be 3 ways: https://www.purebasic.fr/english/viewtopic.php?f=5&t=47987
ExamineDirectory(), PowerShell, ExplorerListGadget
I don't know how up-to-date they are.


Top
 Profile  
Reply with quote  
 Post subject: Re: Count files in folder
PostPosted: Thu Oct 11, 2018 10:13 am 
Offline
User
User

Joined: Sun Nov 23, 2014 1:18 pm
Posts: 24
I use this little code

Code:
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)


Top
 Profile  
Reply with quote  
 Post subject: Re: Count files in folder
PostPosted: Thu Oct 11, 2018 10:21 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Feb 08, 2014 3:26 pm
Posts: 487
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:
RunProgram("cmd", "/k dir | findstr fichier", "C:\Windows", #PB_Program_Wait)
Code:
 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:


Top
 Profile  
Reply with quote  
 Post subject: Re: Count files in folder
PostPosted: Thu Oct 11, 2018 10:23 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Mar 10, 2013 3:01 pm
Posts: 562
Location: Portugal
Thank you, my friends!


Top
 Profile  
Reply with quote  
 Post subject: Re: Count files in folder
PostPosted: Thu Oct 11, 2018 11:56 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1571
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:
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: Count files in folder
PostPosted: Thu Oct 11, 2018 1:32 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Apr 26, 2003 2:15 pm
Posts: 741
Location: Cuernavaca, Mexico
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Count files in folder
PostPosted: Thu Oct 11, 2018 1:43 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1571
blueb wrote:
code fails if the are spaces in the foldername

Fixed (see edited post). Thanks! ;)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye