Page 1 of 1
Count files in folder
Posted: Thu Oct 11, 2018 9:54 am
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!
Re: Count files in folder
Posted: Thu Oct 11, 2018 10:01 am
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.
Re: Count files in folder
Posted: Thu Oct 11, 2018 10:13 am
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)
Re: Count files in folder
Posted: Thu Oct 11, 2018 10:21 am
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)
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.
Re: Count files in folder
Posted: Thu Oct 11, 2018 10:23 am
by marcoagpinto
Thank you, my friends!
Re: Count files in folder
Posted: Thu Oct 11, 2018 11:56 am
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\")
Re: Count files in folder
Posted: Thu Oct 11, 2018 1:32 pm
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)
Re: Count files in folder
Posted: Thu Oct 11, 2018 1:43 pm
by Dude
blueb wrote:code fails if the are spaces in the foldername
Fixed (see edited post). Thanks!